点击眼睛切换EditText明文、密文显示

setTransformationMethod是TextView的一个方法,EditText继承于TextView自然可以使用
这个方法是用来设置其中text的转换显示
接收的参数是TransformationMethod接口,系统给了我们几个默认实现:
HideReturnsTransformationMethod隐藏回车
SingleLineTransformationMethod不能用换行回车
PasswordTransformationMethod密码类型
ReplacementTransformationMethod抽象类,前面两个都是继承于这个抽象类,很明显就是替换,我们可以自己去继承这个类实现自己的TransformationMethod
xml部分文件:



        

        

        

    

    

        

        
        

    

    

        

        
        
    

主要代码:

//明文显示         
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
 //密文显示  
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
//光标在最后
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);
        }
     }
});

你可能感兴趣的:(点击眼睛切换EditText明文、密文显示)