これは古いものかもしれませんが、使用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>
inputType
上記のコードはXML で定義されていません
EditText password = (EditText) findViewById(R.id.password);
password.setTransformationMethod(new PasswordTransformationMethod());
また、Java では、入力タイプsetTransformationMethod
のプロパティを取得するのに役立ちますtextPassword
。カスタム フォント スタイルも気に入っています。
しかし、27.1.1 サポート ライブラリでは、すべての API レベルで以下のクラッシュが発生しました。
java.lang.NullPointerException: null オブジェクト参照で仮想メソッド 'void android.support.design.widget.CheckableImageButton.setChecked(boolean)' を呼び出そうとしています
これは onRestoreInstanceState
内部TextInputLayout
クラスが原因でクラッシュしていました。
再現手順:パスワードの表示を切り替え、アプリを最小化して、最近使用したアプリから開きます。うーん、クラッシュした!
必要なのは、デフォルトのパスワード切り替えオプション (サポート ライブラリを使用) と、パスワード入力フィールドのカスタム フォントだけです。
しばらくして、以下のようにして考え出した、
<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); // set to password text input layout
上記のJavaコードでは、
ユーザー名からカスタム書体を取得し、パスワード フィールドにEditText
適用しました。プロパティを取得するためTextInputLayout
、書体を明示的にパスワードに設定する必要はありません。EditText
TextInputLayout
また、取り外しましたpassword.setTransformationMethod(new PasswordTransformationMethod());
こうすることで、passwordToggleEnabled
動作し、カスタムフォントも適用され、クラッシュとはさようなら。この問題は、今後のサポート リリースで修正されることを願っています。