Android中EditText顯示明文與密碼的兩種方式
更新時間:2016年08月07日 14:04:31 作者:森林森
這篇文章主要介紹了Android中EditText顯示明文與密碼的兩種方式,非常不錯,具有參考借鑒價值,需要的盆友一起學習吧
效果圖如下所述:

布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="liu.basedemo.MainActivity"> <EditText android:id="@+id/etUsername" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:hint="請輸入用戶名" android:textColor="#000000" android:textColorHint="#55000000" android:textSize="20sp"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical"> <EditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:hint="請輸入密碼" android:inputType="textPassword" android:textColor="#000000" android:textColorHint="#55000000" android:textSize="20sp"/> <CheckBox android:checked="false" android:id="@+id/cbDisplayPassword" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:button="@drawable/selector_password"/> </RelativeLayout> </LinearLayout> selector <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/cb_checked" android:state_checked="true"/> <item android:drawable="@mipmap/cb_normaled" android:state_checked="false"/> </selector>
EditText顯示明文與密碼的兩種方式如下所述:
第一種方式
private void initListener() {
mCbDisplayPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "onCheckedChanged: "+isChecked);
if(isChecked){
//選擇狀態(tài) 顯示明文--設置為可見的密碼
mEtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}else {
//默認狀態(tài)顯示密碼--設置文本 要一起寫才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
mEtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
});
}
第二種方式
private void initListener() {
mCbDisplayPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "onCheckedChanged: "+isChecked);
if(isChecked){
//選擇狀態(tài) 顯示明文--設置為可見的密碼
//mEtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
/**
* 第二種
*/
mEtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else {
//默認狀態(tài)顯示密碼--設置文本 要一起寫才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
//mEtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
/**
* 第二種
*/
mEtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
}
以上所述是小編給大家介紹的Android中EditText顯示明文與密碼的兩種方式,希望對大家有所幫助,如果大家想了解更多內(nèi)容敬請關注腳本之家!
您可能感興趣的文章:
- Android實現(xiàn)密碼隱藏和顯示
- Android實現(xiàn)顯示和隱藏密碼功能的示例代碼
- Android 登錄頁面的實現(xiàn)代碼(密碼顯示隱藏、EditText 圖標切換、限制輸入長度)
- Android中實現(xiàn)密碼的隱藏和顯示的示例
- Android EditText密碼的隱藏和顯示功能
- Android 密碼 顯示與隱藏功能實例
- Android中實現(xiàn)EditText密碼顯示隱藏的方法
- Android文本輸入框(EditText)輸入密碼時顯示與隱藏
- Android實現(xiàn)動態(tài)顯示或隱藏密碼輸入框的內(nèi)容
- Android開發(fā)EditText實現(xiàn)密碼顯示隱藏
相關文章
Android Studio 新建項目通過git上傳到碼云圖文教程詳解
本文通過圖文并茂的方式給大家介紹了Android Studio 新建項目通過git上傳到碼云的方法,需要的朋友可以參考下2017-11-11
Android Socket服務端與客戶端用字符串的方式互相傳遞圖片的方法
這篇文章主要介紹了Android Socket服務端與客戶端用字符串的方式互相傳遞圖片的方法的相關資料,需要的朋友可以參考下2016-05-05
Android中CountDownTimer 實現(xiàn)倒計時功能
本篇文章主要介紹了Android中CountDownTimer 實現(xiàn)倒計時功能,CountDownTimer 是android 自帶的一個倒計時類,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05

