Android開(kāi)發(fā)EditText實(shí)現(xiàn)密碼顯示隱藏
最近在做一個(gè)登錄、注冊(cè)頁(yè)面,里面需要顯示或隱藏密碼,故做了一個(gè)簡(jiǎn)單的顯示和隱藏功能。
關(guān)鍵類(lèi)TextView.setTransformationMethod(TransformationMethod method),其中TransformationMethod 有兩個(gè)子類(lèi):
- HideReturnsTransformationMethod 隱藏回車(chē)
- PasswordTransformationMethod 密碼類(lèi)型
關(guān)鍵代碼:
@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屬性存儲(chǔ)當(dāng)前密碼輸入框的類(lèi)型,1是密碼類(lèi)型,2是顯示類(lèi)型。布局組件關(guān)鍵代碼如下:
<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="請(qǐng)輸入密碼" ? ? ? ? ? ? ? ? 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>
截圖:


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開(kāi)發(fā)EditText禁止輸入監(jiān)聽(tīng)及InputFilter字符過(guò)濾
- Android中EditText光標(biāo)的顯示與隱藏方法
- Android TextWatcher三個(gè)回調(diào)以及監(jiān)聽(tīng)EditText的輸入案例詳解
- Android EditText輸入框?qū)崿F(xiàn)下拉且保存最近5個(gè)歷史記錄思路詳解
- Android使用EditText小技巧匯總
- Android EditText隨輸入法一起移動(dòng)并懸浮在輸入法之上的示例代碼
- Android EditText每4位自動(dòng)添加空格效果
- Android編輯框EditText與焦點(diǎn)變更監(jiān)視器及文本變化監(jiān)視器實(shí)現(xiàn)流程詳解
相關(guān)文章
Android入門(mén)之利用OKHttp實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能
這篇文章主要為大家詳細(xì)介紹了Android如何使用OKHttp多線程制作像迅雷一樣的斷點(diǎn)續(xù)傳功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-01-01
Android Webview添加網(wǎng)頁(yè)加載進(jìn)度條實(shí)例詳解
這篇文章主要介紹了Android Webview添加網(wǎng)頁(yè)加載進(jìn)度條實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android中Listview點(diǎn)擊item不變顏色及設(shè)置listselector 無(wú)效的解決方案
這篇文章主要介紹了Android中Listview點(diǎn)擊item不變顏色及設(shè)置listselector 無(wú)效的原因及解決方案,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android實(shí)現(xiàn)簡(jiǎn)易瀏覽器遇到問(wèn)題的解決方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易瀏覽器遇到的一系列問(wèn)題的解決方法,感興趣的小伙伴們可以參考一下2016-06-06
Android如何讓APP無(wú)法在指定的系統(tǒng)版本上運(yùn)行(實(shí)現(xiàn)方法)
這篇文章主要介紹了Android如何讓APP無(wú)法在指定的系統(tǒng)版本上運(yùn)行(實(shí)現(xiàn)方法),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Android?實(shí)現(xiàn)卡片堆疊錢(qián)包管理動(dòng)畫(huà)效果
這篇文章主要介紹了Android?實(shí)現(xiàn)卡片堆疊錢(qián)包管理動(dòng)畫(huà)效果,實(shí)現(xiàn)思路是在動(dòng)畫(huà)回調(diào)中requestLayout?實(shí)現(xiàn)動(dòng)畫(huà)效果,用Bounds?對(duì)象記錄每一個(gè)CardView?對(duì)象的初始位置,當(dāng)前位置,運(yùn)動(dòng)目標(biāo)位置,需要的朋友可以參考下2022-07-07
Android的APK應(yīng)用簽名機(jī)制以及讀取簽名的方法
這篇文章主要介紹了Android的APK應(yīng)用簽名機(jī)制以及讀取簽名的方法,這里作者推薦使用Java自帶的API進(jìn)行APK簽名的讀取,需要的朋友可以參考下2016-02-02

