日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android開發EditText實現密碼顯示隱藏_Android

作者:伊文他媽 ? 更新時間: 2022-09-27 編程語言

最近在做一個登錄、注冊頁面,里面需要顯示或隱藏密碼,故做了一個簡單的顯示和隱藏功能。

關鍵類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>

截圖:

原文鏈接:https://blog.csdn.net/xll712/article/details/120885723

欄目分類
最近更新