[英]How to fix TextInputLayout passwordToggle?
我创建了一个带有验证的简单登录页面,但是当我想使用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();
所以这就是我如何实现我的代码并且应用程序不断停止所以我做错了什么?
谢谢!
使用getEditText
方法获取用于文本输入的EditText
:
TextInputLayout test2= findViewById(R.id.test2);
String passwordInput = test2.getEditText().getText().toString().trim();
您必须首先通过为其分配一个 ID 来找到视图,如下所示
<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);
然后你可以得到输入的文本,比如
String passwordInput = etPassword.getText().toString().trim();
为 TextInputEditText 分配一个 ID
<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);
然后你可以得到输入的文本,比如
String mPassword = etPassword.getText().toString().trim();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.