Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 密碼欄位動態顯示與隱藏的切換實作

編輯:Android 開發實例

很多 App 都提供「小眼睛」按鈕,讓使用者隨時切換密碼的明文與掩碼顯示。這個功能可以透過 EditText 的 setTransformationMethod() 來實現。

核心只有兩個方法:HideReturnsTransformationMethod 讓文字明文顯示,PasswordTransformationMethod 則把每個字元顯示為點號。

 private Button mBtnPassword;
 private EditText mEtPassword;
 private boolean mbDisplayFlg = false;

 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   mEtPassword = (EditText)findViewById(R.id.password);
   mBtnPassword = (Button)findViewById(R.id.btnPassword);

   mBtnPassword.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
       Log.d("AndroidTest", "mbDisplayFlg = " + mbDisplayFlg);
       if (!mbDisplayFlg) {
         // 顯示密碼原文,例如 "123456"
         mEtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
       } else {
         // 隱藏密碼,顯示 "."
         mEtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
       }
       mbDisplayFlg = !mbDisplayFlg;
       mEtPassword.postInvalidate();
     }
   });
 }

版面配置:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <Button
     android:id="@+id/btnPassword"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="密碼" />

   <EditText
     android:id="@+id/password"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:password="true"
     android:textSize="18sp"
     android:text="123456">
   </EditText>
 </LinearLayout> 

更簡潔的現代寫法

其實不必自己維護 mbDisplayFlg 這個布林旗標,也不必手動呼叫 postInvalidate()。若你的專案使用 AppCompat(幾乎所有現代專案都是),最省事的做法是在版面配置中直接加上:

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    app:passwordToggleEnabled="true" />

或者用 TextInputLayout 包住它:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:passwordToggleEnabled="true">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>

這樣 framework 會自動產生切換圖示、處理狀態與重繪,程式碼完全不用寫。

幾個細節提醒

android:password 已被取代。範例中的 android:password="true" 是舊屬性,官方建議改用 android:inputType="textPassword"(數字密碼用 numberPassword,可見可輸的用 textVisiblePassword),因為 inputType 能同時表達鍵盤類型與顯示方式。

切換後游標位置。更換 TransformationMethod 會讓游標跳到尾端。若使用者在中間編輯後切換,游標位置會改變,體驗略差。需要精細處理時可先記下 getSelectionStart(),切換後用 setSelection() 還原。

明文顯示不宜持續太久。基於安全性考量,多數 App 會在明文顯示數秒後自動恢復掩碼,或使用「按住才顯示」的按壓式互動。若要做自動恢復,用 postDelayed() 排一個 Runnable 即可。

不要把密碼存進偏好設定。順帶一提,切換顯示狀態屬於純 UI 狀態,Activity 重建時會被重置——這其實是好事,能避免使用者離開後螢幕上仍留著明文密碼。希望本文所述對大家的 Android 開發有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved