如何使用自定义图片代替'*'中的EditText密码字段?
How to use custom image instead of '*' in edittext password field?
看图片:
任何答案或暗示将大大AP preciated。
Any answer or hint will be greatly appreciated.
答案来自的本教程以及它涵盖了行为,当一个用户:
The answer comes from this tutorial and it covers a behaviour when a user:
进入登陆界面,键盘会自动打开。
enters into the login screen, keyboard will open automatically.
试图在它,然后文本框的背景变化与明星背景输入值的文本框。
tries to enter value in it then textbox background changes to textbox with star background.
尝试取消/使用返回键键盘上删除输入值,那么文本框的背景将变为文本没有明星的背景。
tries to cancel/delete the input value by using back key on keyboard then textbox background will change to textbox without star background.
你所创建的前两个可绘制
:
然后,根据这种方法,你必须实现你的的EditText
addTextChangedListener
方法。在此之后,作为一个参数,你创建一个 TextWatcher
类的新实例,你实现它的方法:
Then, according to this approach, you have to implement addTextChangedListener
method on your EditText
. After that, as a parameter, you create a new instance of a TextWatcher
class and you implement its methods:
etxtPin1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
if(etxtPin1.getText().toString().trim().length()==1){
etxtPin1.clearFocus();
etxtPin2.requestFocus();
etxtPin1.setBackgroundResource(R.drawable.pin_txt_bg_star);
}
}
});
然后,你必须实现 setOnKeyListener
及其方法安其
:
this.etxtPin1.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent) {
if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (LoginActivity.this.etxtPin2.getText().length() == 0)) {
etxtPin1.requestFocus();
etxtPin1.setBackgroundResource(R.drawable.pin_txt_bg);
etxtPin1.setText("");
}
return false;
}
});
另一种方法:创建您自己的延伸 PasswordTransformationMethod 。
public class MyPasswordTransformationMethod extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
};
参考:In android怎么样展现到位点的星号(*)的有inputType下为textPassword的EditText?