直接上代码:
publicclass ShowPasswordActivity extends Activity {
private EditText edittext;
private CheckBox checkbox;
/** Called when the activity is first created. */
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*find the object by IDs .*/
edittext = (EditText) findViewById(R.id.et);
checkbox = (CheckBox) findViewById(R.id.cb);
/* add a listener to the CheckBox */
checkbox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
{
publicvoid onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
if(checkbox.isChecked())
{
/* show the password*/
edittext.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
else
{
/* hide the password */
edittext.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
}
}
补充:
if (isChecked){
network_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) ;
}else{
network_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD) ;
}
// 使光标始终在最后位置
Editable etable = network_password.getText();
Selection.setSelection(etable, etable.length());