密碼可見性切換:
在Android支持庫24.2.0中,引入了“密碼可見性切換”到旦。
- TextInputLayout 增加了對密碼可見性切換
什么是密碼可見性切換:
根據(jù)Material Design Guideline 旨巷,使用此圖標(biāo)添忘,我們可以在密碼輸入字段中啟用和禁用密碼可見性斧吐。
當(dāng)可見性圖標(biāo)顯示文本字段時,它指示該字段的輸入是否可見∩烟危可以使用圖標(biāo)打開或關(guān)閉文本字段的可見性:
- 啟用可見性:圖標(biāo)不透明度為54%,密碼可見。
- 禁用可見性:圖標(biāo)不透明度為38%团赁,密碼不可見笋粟。
先上效果圖:
添加“顯示密碼”屬性后:
現(xiàn)在介紹一下具體使用方法:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:focusable="true"
android:focusableInTouchMode="true"
>
<android.support.design.widget.TextInputLayout
android:id="@+id/layout_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/colorHint"
>
<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
android:text="aaaa"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout_email"
android:textColorHint="@color/colorHint"
app:passwordToggleDrawable="@mipmap/pwd_visiable"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:hint="@string/hint_password"
android:inputType="textPassword"
android:text="aaaaa"
/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/layout_email"
android:layout_marginTop="104dp"
android:background="@color/buttoncolor"
android:text="登錄"
android:textColor="@color/textColor"/>
</RelativeLayout>
當(dāng)然殿漠,在我們實(shí)際開發(fā)中"顯示/隱藏圖標(biāo)"可能需要有狀態(tài)變化的失尖,所以,上面代碼就不能滿足我們的需求了,這時候我們就需要稍微修改一下代碼薯鼠。
我們先刪除xml文件中“app:passwordToggleDrawable="@mipmap/pwd_visiable"”這行代碼,然后我們在Activity中動態(tài)添加“顯示/隱藏”圖標(biāo)。
TextInputLayout mLayout = (TextInputLayout) findViewById(R.id.layout_password);
mLayout.setPasswordVisibilityToggleDrawable(R.drawable.pwd_selector);
pwd_selector.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/pwd_visiable" android:state_checked="true"/>
<item android:drawable="@mipmap/pwd_gone" android:state_checked="false"/>
<item android:drawable="@mipmap/pwd_gone" />
</selector>
使用的資源:
修改后的效果如下:
更多定制:
android.support.design:passwordToggleContentDescription 設(shè)置為密碼輸入可見性切換的內(nèi)容描述的文本。
android.support.design:passwordToggleDrawable 可用作密碼輸入可見度切換圖標(biāo)胆胰。
android.support.design:passwordToggleEnabled 當(dāng)EditText有密碼時狞贱,視圖是否顯示切換。
android.support.design:passwordToggleTint 用于密碼輸入可見性的圖標(biāo)toggleMay可以是一個顏色值
android.support.design:passwordToggleTintMode 混合模式用于應(yīng)用背景色調(diào)蜀涨。
可從文檔中查看更多瞎嬉。
好了蝎毡,到這里,就全部結(jié)束了佑颇。