編輯: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 開發有所幫助。
Android 自訂 EditText 與 AutoCompleteTextView 的文字選取底色
多數 Android ROM 上,文字被選取時的背景色是清爽的鮮綠色,視覺上不難看。但某些三星機型卻是藍色,和整體介面放在一起格外突兀。好消息是,這個顏色完全可以透過程式
Android 自定義權限的實作方式
自定義權限是 Android 組件保護的重要機制。透過自定義權限,開發者可限制特定 Activity、Service 或其他組件只能被持有指定權限的應用呼叫。定義權限的應
Android 的 Style 與 Theme:一次搞懂主題與樣式的分層設計
現在幾乎每家公司都會在 Android 上部署自己的客戶端,而使用者對介面美觀度的期待也越來越高。Style 與 Theme 就是 Android 用來統一視覺風格、提升
Android 登錄頁面實作:表單驗證與錯誤次數控制
登錄頁面的基本需求 登錄畫面通常包含使用者名稱、密碼與登錄按鈕。為了避免暴力嘗試,還可以加入錯誤次數限制。當次數用完時,停用登錄按鈕或提示使用者稍後再試。 建立輸入欄位