Android密码明文密文切换

Android密码明文密文切换,注册的时候用的比较多。

效果动画:

Android密码明文密文切换_第1张图片

MainActivity

package com.example.shen.passwordtest;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView(){
        final EditText etPassword=(EditText) findViewById(R.id.et_password);
        final CheckBox cbEye=(CheckBox) findViewById(R.id.cb_eye);
        cbEye.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    //设置为明文显示
                    etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    //设置为密文显示
                    etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                //光标在最后显示
                etPassword.setSelection(etPassword.length());
            }
        });
    }


}
activity_main.xml


    
        
        
        
        
    

pic_eye_selector.xml



    
    




你可能感兴趣的:(Android)