android EditText顯示隱藏密碼

原文鏈接:https://blog.csdn.net/androidxm/article/details/77884856

 

EditText顯示(隱藏)密碼  兩種方式

第一種: TextInputLayout設置passwordToggleEnabled屬性

 

<android.support.design.widget.TextInputLayout

app:passwordToggleEnabled="true"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<android.support.design.widget.TextInputEditText

android:inputType="textPassword"

android:hint="密碼"

android:imeOptions="actionDone"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

</android.support.design.widget.TextInputLayout>

可以通過設置屬性 app:passwordToggleDrawable 改變圖標

 

第二種: 通過setTransformationMethod 方法設置

 

<CheckBox

android:id="@+id/checkbox"

android:button="@drawable/password_selector"

android:layout_gravity="center"

android:layout_width="wrap_content"

android:layout_height="match_parent" />

 

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

mEtPassword.setTransformationMethod(isChecked? PasswordTransformationMethod.getInstance(): HideReturnsTransformationMethod.getInstance());

mEtPassword.setSelection(mEtPassword.length());

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章