Android開發(fā)EditText實現(xiàn)密碼顯示隱藏
更新時間:2022年08月02日 11:34:24 作者:伊文他媽
這篇文章主要為大家詳細介紹了Android開發(fā)EditText實現(xiàn)密碼顯示隱藏,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
最近在做一個登錄、注冊頁面,里面需要顯示或隱藏密碼,故做了一個簡單的顯示和隱藏功能。
關鍵類TextView.setTransformationMethod(TransformationMethod method),其中TransformationMethod 有兩個子類:
- HideReturnsTransformationMethod 隱藏回車
- PasswordTransformationMethod 密碼類型
關鍵代碼:
@OnClick(R.id.iv_psw_eye)
? ? void clickPswEye() {
? ? ? ? int tag = Integer.parseInt(pswEyeIV.getTag().toString());
? ? ? ? if (tag == 1) {//顯示密碼
? ? ? ? ? ? pswEyeIV.setTag(2);
? ? ? ? ? ? pswEyeIV.setImageResource(R.mipmap.icon_psw_not_eye);
? ? ? ? ? ? passwordET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
? ? ? ? } else {//隱藏密碼
? ? ? ? ? ? pswEyeIV.setTag(1);
? ? ? ? ? ? pswEyeIV.setImageResource(R.mipmap.icon_psw_eye);
? ? ? ? ? ? passwordET.setTransformationMethod(PasswordTransformationMethod.getInstance());
? ? ? ? }
? ? }我用ImageView的Tag屬性存儲當前密碼輸入框的類型,1是密碼類型,2是顯示類型。布局組件關鍵代碼如下:
<RelativeLayout ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="45dp" ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? android:layout_marginTop="26dp" ? ? ? ? ? ? android:background="@drawable/bg_edit_login" ? ? ? ? ? ? android:paddingLeft="26dp"> ? ? ? ? ? ? <EditText ? ? ? ? ? ? ? ? android:id="@+id/password_ET" ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:layout_toLeftOf="@+id/iv_psw_eye" ? ? ? ? ? ? ? ? android:background="@null" ? ? ? ? ? ? ? ? android:gravity="center_vertical" ? ? ? ? ? ? ? ? android:hint="請輸入密碼" ? ? ? ? ? ? ? ? android:inputType="textPassword" ? ? ? ? ? ? ? ? android:maxLines="1" ? ? ? ? ? ? ? ? android:textColor="@color/color_666666" ? ? ? ? ? ? ? ? android:textColorHint="@color/color_999999" ? ? ? ? ? ? ? ? android:textSize="13sp" /> ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:id="@+id/iv_psw_eye" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:layout_alignParentRight="true" ? ? ? ? ? ? ? ? android:paddingLeft="20dp" ? ? ? ? ? ? ? ? android:paddingRight="26dp" ? ? ? ? ? ? ? ? android:src="@mipmap/icon_psw_eye" ? ? ? ? ? ? ? ? android:tag="1" /> </RelativeLayout>
截圖:


以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android開發(fā)EditText禁止輸入監(jiān)聽及InputFilter字符過濾
- Android中EditText光標的顯示與隱藏方法
- Android TextWatcher三個回調以及監(jiān)聽EditText的輸入案例詳解
- Android EditText輸入框實現(xiàn)下拉且保存最近5個歷史記錄思路詳解
- Android使用EditText小技巧匯總
- Android EditText隨輸入法一起移動并懸浮在輸入法之上的示例代碼
- Android EditText每4位自動添加空格效果
- Android編輯框EditText與焦點變更監(jiān)視器及文本變化監(jiān)視器實現(xiàn)流程詳解
相關文章
Android入門之利用OKHttp實現(xiàn)斷點續(xù)傳功能
這篇文章主要為大家詳細介紹了Android如何使用OKHttp多線程制作像迅雷一樣的斷點續(xù)傳功能,文中的示例代碼講解詳細,感興趣的可以了解一下2023-01-01
Android中Listview點擊item不變顏色及設置listselector 無效的解決方案
這篇文章主要介紹了Android中Listview點擊item不變顏色及設置listselector 無效的原因及解決方案,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
Android如何讓APP無法在指定的系統(tǒng)版本上運行(實現(xiàn)方法)
這篇文章主要介紹了Android如何讓APP無法在指定的系統(tǒng)版本上運行(實現(xiàn)方法),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02

