Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android EditText 密碼顯示與隱藏

編輯:關於 Android 程式設計

在登入、註冊或修改密碼頁面中,讓使用者能切換密碼明文與密文顯示,是常見且貼心的互動設計。自從 Design Support Library 推出後,可以透過 TextInputLayout 快速完成這個效果。

基本用法

只要將 EditText 包在 TextInputLayout 中,並設定密碼輸入類型,再開啟密碼切換屬性即可:

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:passwordToggleEnabled="true">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>

原理說明

當 EditText 的輸入類型為密碼時,TextInputLayout 會在右側顯示眼睛圖示。使用者點擊後,即可在明文與密文之間切換,提升輸入驗證的便利性。

注意事項

若專案中的 Design 元件版本較舊,可能不支援此屬性。此時可先升級相依元件,或自行以圖片按鈕搭配輸入法的密碼轉換方法實作。

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