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="請(qǐng)輸入用戶(hù)名" 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="請(qǐng)輸入密碼" 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) 顯示明文--設(shè)置為可見(jiàn)的密碼 mEtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); }else { //默認(rèn)狀態(tài)顯示密碼--設(shè)置文本 要一起寫(xiě)才能起作用 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) 顯示明文--設(shè)置為可見(jiàn)的密碼 //mEtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); /** * 第二種 */ mEtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); }else { //默認(rèn)狀態(tài)顯示密碼--設(shè)置文本 要一起寫(xiě)才能起作用 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顯示明文與密碼的兩種方式,希望對(duì)大家有所幫助,如果大家想了解更多內(nèi)容敬請(qǐng)關(guān)注腳本之家!
- Android實(shí)現(xiàn)密碼隱藏和顯示
- Android實(shí)現(xiàn)顯示和隱藏密碼功能的示例代碼
- Android 登錄頁(yè)面的實(shí)現(xiàn)代碼(密碼顯示隱藏、EditText 圖標(biāo)切換、限制輸入長(zhǎng)度)
- Android中實(shí)現(xiàn)密碼的隱藏和顯示的示例
- Android EditText密碼的隱藏和顯示功能
- Android 密碼 顯示與隱藏功能實(shí)例
- Android中實(shí)現(xiàn)EditText密碼顯示隱藏的方法
- Android文本輸入框(EditText)輸入密碼時(shí)顯示與隱藏
- Android實(shí)現(xiàn)動(dòng)態(tài)顯示或隱藏密碼輸入框的內(nèi)容
- Android開(kāi)發(fā)EditText實(shí)現(xiàn)密碼顯示隱藏
相關(guān)文章
Android百度地圖應(yīng)用之基本地圖功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android百度地圖應(yīng)用之基本地圖功能實(shí)現(xiàn)的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06Android編程自定義菜單實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Android編程自定義菜單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android自定義菜單的布局、動(dòng)畫(huà)及功能相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2017-02-02Android Studio 新建項(xiàng)目通過(guò)git上傳到碼云圖文教程詳解
本文通過(guò)圖文并茂的方式給大家介紹了Android Studio 新建項(xiàng)目通過(guò)git上傳到碼云的方法,需要的朋友可以參考下2017-11-11OpenGL實(shí)現(xiàn)Bezier曲線的方法示例
今天小編就為大家分享一篇關(guān)于OpenGL實(shí)現(xiàn)Bezier曲線的方法示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04Android11及以上文件讀寫(xiě)權(quán)限申請(qǐng)?jiān)敿?xì)介紹
安卓11改變了此前安卓系統(tǒng)對(duì)于文件管理的規(guī)則,在安卓 11 上,文件讀寫(xiě)變成了特殊權(quán)限,下面這篇文章主要給大家介紹了關(guān)于Android11及以上文件讀寫(xiě)權(quán)限申請(qǐng)的相關(guān)資料,需要的朋友可以參考下2022-08-08Android Socket服務(wù)端與客戶(hù)端用字符串的方式互相傳遞圖片的方法
這篇文章主要介紹了Android Socket服務(wù)端與客戶(hù)端用字符串的方式互相傳遞圖片的方法的相關(guān)資料,需要的朋友可以參考下2016-05-05Android中CountDownTimer 實(shí)現(xiàn)倒計(jì)時(shí)功能
本篇文章主要介紹了Android中CountDownTimer 實(shí)現(xiàn)倒計(jì)時(shí)功能,CountDownTimer 是android 自帶的一個(gè)倒計(jì)時(shí)類(lèi),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05