您的位置:首页 > 移动开发 > Android开发

Android 打勾显示输入的密码

2015-09-15 15:04 155 查看
直接上代码:

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());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: