我知道这可能是较旧的问题,但当我同时使用InputType
和app:passwordToggleEnabled="true"
时,我遇到了与此问题相关的情况。
因此,我写下这篇文章,希望能够帮助到某些人。
我想在密码输入字段中使用自定义字体,并使用app:passwordToggleEnabled
选项。但是,在27.1.1(编写本文时)支持库中,它会崩溃。
所以代码如下:
<android.support.design.widget.TextInputLayout
android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_10dp"
android:layout_marginTop="@dimen/_32dp"
android:hint="@string/current_password"
android:textColorHint="@color/hint_text_color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:passwordToggleEnabled="true"
app:passwordToggleTint="@color/black">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|left"
android:maxLines="1"
android:textAlignment="viewStart"
android:textColor="@color/black"
android:textColorHint="@color/camel"
android:textSize="@dimen/txt_16sp"
app:font_style="regular"
app:drawableEnd="@drawable/ic_remove_eye" />
</android.support.design.widget.TextInputLayout>
以上代码在XML中没有定义inputType
EditText password = (EditText) findViewById(R.id.password)
password.setTransformationMethod(new PasswordTransformationMethod())
在Java中,
setTransformationMethod
方法可以帮助我获取
textPassword
输入类型的属性,并且我可以使用自定义字体样式。但是在所有API级别中,使用27.1.1支持库时发生了下面提到的崩溃。
java.lang.NullPointerException: 尝试调用空对象引用的'void android.support.design.widget.CheckableImageButton.setChecked(boolean)'方法
这是由于
TextInputLayout
类中的
onRestoreInstanceState
引起的。
重现步骤: 切换密码可见性,最小化应用并从最近的应用程序中打开。噢,出问题了!
我需要的只是默认的密码切换选项(使用支持库)和密码输入字段中的自定义字体。
经过一段时间的研究,我找到了以下解决方法:
<android.support.design.widget.TextInputLayout
android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_10dp"
android:layout_marginTop="@dimen/_32dp"
android:hint="@string/current_password"
android:textColorHint="@color/hint_text_color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:passwordToggleEnabled="true"
app:passwordToggleTint="@color/black">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|left"
android:maxLines="1"
android:textAlignment="viewStart"
android:textColor="@color/black"
android:textColorHint="@color/camel"
android:textSize="@dimen/txt_16sp"
app:font_style="regular"
app:drawableEnd="@drawable/ic_remove_eye"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
在XML中,添加了
android:inputType="textPassword"
。
TextInputLayout inputPassword = findViewById(R.id.input_password)
EditText password = findViewById(R.id.password)
EditText userName = findViewById(R.id.user_name)
// Get the typeface of user name or other edit text
Typeface typeface = userName.getTypeface()
if (typeface != null)
inputLayout.setTypeface(typeface)
在上面的Java代码中,我从用户名
EditText
获取了自定义字体,并将其应用于密码字段的
TextInputLayout
。现在,您不需要显式设置密码
EditText
的字体,因为它将获取
TextInputLayout
的属性。
此外,我删除了
password.setTransformationMethod(new PasswordTransformationMethod());
通过这种方式,
passwordToggleEnabled
正常工作,并且自定义字体也得到应用,崩溃问题也解决了。希望这个问题能在即将发布的支持版本中修复。