如何修复TextInputLayout passwordToggle?

问题描述 投票:1回答:3

我创建了一个带有验证的简单登录页面,但当我想使用passwordToggleEnabled应用程序崩溃并且不知道为什么出现0个错误时,密码部分存在问题。

我的项目在API 26上。

我已经在依赖项中添加了构建gradle:

    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'

并且在XML文件中,这是我的密码输入代码:

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/test2"
            android:layout_below="@id/usernameEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            app:errorEnabled="true"
            app:passwordToggleEnabled="true">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Your Password"
                android:inputType="textPassword" />
        </com.google.android.material.textfield.TextInputLayout>

并且在MainActivity.java中,这是我如何调用pwd的ID:

String passwordInput = test2.getText().toString().trim();

所以这就是实现我的代码的方式,应用程序不断停止,所以我做错了吗?

谢谢!

java android android-textinputlayout android-support-design android-textinputedittext
3个回答
1
投票

使用方法getEditText获得用于文本输入的getEditText

EditText

1
投票

您必须首先通过为其分配ID来查找视图,然后再找到它

TextInputLayout test2= findViewById(R.id.test2);
String passwordInput = test2.getEditText().getText().toString().trim();

然后在这样的活动中找到它

  <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/etPassword"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Your Password"
                    android:inputType="textPassword" />

然后您可以得到输入的文本,如

 TextInputEditText etPassword= findViewById(R.id.etPassword);

0
投票

将ID分配给TextInputEditText

String passwordInput = etPassword.getText().toString().trim();

然后在这样的活动中找到它

  <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/etPassword"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Your Password"
                    android:inputType="textPassword" />

然后您可以得到输入的文本,如

EditText etPassword= findViewById(R.id.etPassword);
© www.soinside.com 2019 - 2024. All rights reserved.