點(diǎn)擊眼睛切換EditText明文、密文顯示

setTransformationMethod是TextView的一個(gè)方法,EditText繼承于TextView自然可以使用
這個(gè)方法是用來設(shè)置其中text的轉(zhuǎn)換顯示
接收的參數(shù)是TransformationMethod接口,系統(tǒng)給了我們幾個(gè)默認(rèn)實(shí)現(xiàn):
HideReturnsTransformationMethod隱藏回車
SingleLineTransformationMethod不能用換行回車
PasswordTransformationMethod密碼類型
ReplacementTransformationMethod抽象類,前面兩個(gè)都是繼承于這個(gè)抽象類,很明顯就是替換,我們可以自己去繼承這個(gè)類實(shí)現(xiàn)自己的TransformationMethod
xml部分文件:

<RelativeLayout
        android:id="@+id/olddiv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/appname"
        android:layout_marginHorizontal="16dp"
        android:layout_marginTop="10dp">

        <EditText
            android:id="@+id/editOldPass"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@drawable/sharp_text_layout"
            android:hint="請輸入原始密碼"
            android:inputType="textPassword"
            android:paddingLeft="110dp"
            android:shadowDx="@color/colorBlack"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@drawable/sharp_text_layout"
            android:text="原 密 碼"
            android:textColor="@color/colorBlack"
            android:textSize="14sp" />

        <CheckBox
            android:id="@+id/password_check1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="20dp"
            android:checked="false"
            android:button="@mipmap/eye1"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/newdiv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/appname"
        android:layout_marginHorizontal="16dp"
        android:layout_marginTop="4dp">

        <EditText
            android:id="@+id/editNewPass"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_centerVertical="true"
            android:background="@drawable/sharp_text_layout"
            android:hint="請輸入新密碼"
            android:inputType="textPassword"
            android:paddingLeft="110dp"
            android:textColor="@color/colorBlack"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@drawable/sharp_text_layout"
            android:text="新 密 碼"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />
        <CheckBox
            android:id="@+id/password_check2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:checked="false"
            android:layout_marginRight="20dp"
            android:button="@mipmap/eye1"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/newsdiv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/appname"
        android:layout_marginHorizontal="16dp"
        android:layout_marginTop="4dp">

        <EditText
            android:id="@+id/editNewPassAgain"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@drawable/sharp_text_layout"
            android:hint="請確認(rèn)新密碼"
            android:inputType="textPassword"
            android:paddingLeft="110dp"
            android:textColor="@color/colorBlack"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@drawable/sharp_text_layout"
            android:text="確認(rèn)密碼"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />
        <CheckBox
            android:id="@+id/password_check3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="20dp"
            android:checked="false"
            android:button="@mipmap/eye1"/>
    </RelativeLayout>

主要代碼:

//明文顯示         
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
 //密文顯示  
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
//光標(biāo)在最后
editText.setSelection(editText.length()); 

java部分文件:

EditText oldEditView = updatePassView.findViewById(R.id.editOldPass);
EditText newEditView = updatePassView.findViewById(R.id.editNewPass);
EditText confEditView = updatePassView.findViewById(R.id.editNewPassAgain);
CheckBox password_check1=updatePassView.findViewById(R.id.password_check1);
CheckBox password_check2=updatePassView.findViewById(R.id.password_check2);
CheckBox password_check3=updatePassView.findViewById(R.id.password_check3);

password_check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked){
            oldEditView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            oldEditView.setSelection(oldEditView.length());
            password_check1.setButtonDrawable(R.mipmap.eye2);
         }else {
            oldEditView.setTransformationMethod(PasswordTransformationMethod.getInstance());
            oldEditView.setSelection(oldEditView.length());
            password_check1.setButtonDrawable(R.mipmap.eye1);
         }
    }
});

password_check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         if (isChecked){
             newEditView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
             newEditView.setSelection(oldEditView.length());
             password_check2.setButtonDrawable(R.mipmap.eye2);
         }else {
             newEditView.setTransformationMethod(PasswordTransformationMethod.getInstance());
             newEditView.setSelection(newEditView.length());
             password_check2.setButtonDrawable(R.mipmap.eye1);
         }
      }
});

password_check3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked){
            confEditView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            confEditView.setSelection(confEditView.length());
            password_check3.setButtonDrawable(R.mipmap.eye2);
        }else {
           confEditView.setTransformationMethod(PasswordTransformationMethod.getInstance());
           confEditView.setSelection(confEditView.length());
           password_check3.setButtonDrawable(R.mipmap.eye1);
        }
     }
});
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容