Android開發(fā)EditText實(shí)現(xiàn)密碼顯示隱藏
最近在做一個(gè)登錄、注冊(cè)頁(yè)面,里面需要顯示或隱藏密碼,故做了一個(gè)簡(jiǎn)單的顯示和隱藏功能。
關(guān)鍵類TextView.setTransformationMethod(TransformationMethod method),其中TransformationMethod 有兩個(gè)子類:
- HideReturnsTransformationMethod 隱藏回車
- PasswordTransformationMethod 密碼類型
關(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)前密碼輸入框的類型,1是密碼類型,2是顯示類型。布局組件關(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開發(fā)EditText禁止輸入監(jiān)聽及InputFilter字符過(guò)濾
- Android中EditText光標(biāo)的顯示與隱藏方法
- Android TextWatcher三個(gè)回調(diào)以及監(jiān)聽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中WebView圖片實(shí)現(xiàn)自適應(yīng)的方法
這篇文章主要介紹了Android中WebView圖片實(shí)現(xiàn)自適應(yīng)的方法,涉及Android操作圖片顯示的相關(guān)技巧,需要的朋友可以參考下2015-05-05Android指紋識(shí)別功能深入淺出分析到實(shí)戰(zhàn)(6.0以下系統(tǒng)解決方案)
指紋識(shí)別在現(xiàn)實(shí)應(yīng)用中已經(jīng)很多了,本篇文章主要介紹了Android指紋識(shí)別功能,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11Android實(shí)現(xiàn)購(gòu)物車添加商品動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)購(gòu)物車添加商品動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06android實(shí)現(xiàn)在圖標(biāo)上顯示數(shù)字
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)在圖標(biāo)上顯示數(shù)字,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android 通過(guò)onDraw實(shí)現(xiàn)在View中繪圖操作的示例
以下是對(duì)Android通過(guò)onDraw實(shí)現(xiàn)在View中繪圖操作的示例代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-07-07Android中的廣播、服務(wù)、數(shù)據(jù)庫(kù)、通知、包等術(shù)語(yǔ)的原理和介紹(圖解)
這篇文章主要介紹了Android中的廣播、服務(wù)、數(shù)據(jù)庫(kù)、通知、包等術(shù)語(yǔ)的原理和介紹(圖解),本文用圖文方式總結(jié)了Android中的一些開發(fā)術(shù)語(yǔ),需要的朋友可以參考下2014-10-10Android實(shí)現(xiàn)圖片轉(zhuǎn)高斯模糊以及高斯模糊布局
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片轉(zhuǎn)高斯模糊的方法,以及高斯模糊布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08