1.先看一下效果:
2.现在app很多都有账号,在用户输入密码时可能用到密码显示与隐藏,有两种方式可以实现,这里我写个demo记录一下:
方法一:
private CheckBox cbox;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
cbox = (CheckBox) findViewById(R.id.showPwdCB);
cbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (cbox.isChecked()) {
et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
et.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
}
方法二:
private CheckBox cbox;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
cbox = (CheckBox) findViewById(R.id.showPwdCB);
cbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (cbox.isChecked()) {
et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
et.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
}
方法二:
int passwordLength = et.getText().length(); cbox.setInputType(isChecked ? (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) : (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)); et.setSelection(passwordLength);
我吧源代码放在github上了,有需要的可以去下载:https://github.com/song161013/studio-origin
优快云:下载地址:http://download.youkuaiyun.com/detail/sfwangji/9696575
本文介绍了在App中实现密码输入框显示与隐藏功能的两种方法。一种是通过切换EditText的TransformationMethod,另一种则是改变EditText的InputType。这两种方法都能让用户在输入密码时选择是否显示密码。
1831

被折叠的 条评论
为什么被折叠?



