通过CheckBox.setOnCheckedChangeListener()方法来设置EditText显示密码事件
EditText的setTransformationMethod()方法是用以设置其显示的字符类型。
HideReturnsTransformationMethod.getInstance():正常显示所输入的密码字符;
PasswordTransformationMethod.getinstance():隐藏输入的字符,恢复为密码输入状态。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_transformation_method); ((CheckBox) findViewById(R.id.cb)) .setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { ((EditText) findViewById(R.id.et)) .setTransformationMethod(HideReturnsTransformationMethod .getInstance()); } else { ((EditText) findViewById(R.id.et)) .setTransformationMethod(PasswordTransformationMethod .getInstance()); } } }); }