Android 如何为edittext密码(如axis bank应用程序)使用自定义图像
如何在edittext密码字段中使用自定义图像而不是“*” 如图所示:Android 如何为edittext密码(如axis bank应用程序)使用自定义图像,android,android-edittext,Android,Android Edittext,如何在edittext密码字段中使用自定义图像而不是“*” 如图所示: 任何答案或提示都将不胜感激。我认为简单的方法是,您必须在xml布局中定义6-edittext。 并使用自定义背景分辨率设置宽度和高度 像 <?xml version="1.0" encoding="utf-8"?> 并读取所有编辑文本值。 我不确定,但它会对您有所帮助。答案来自,它涵盖了当用户: 进入登录屏幕,键盘将自动打开 尝试在其中输入值,然后文本框背景更改为具有星形背景的文本框 尝试使用键
任何答案或提示都将不胜感激。我认为简单的方法是,您必须在xml布局中定义6-edittext。 并使用自定义背景分辨率设置宽度和高度 像
<?xml version="1.0" encoding="utf-8"?>
并读取所有编辑文本值。
我不确定,但它会对您有所帮助。答案来自,它涵盖了当用户:
- 进入登录屏幕,键盘将自动打开
- 尝试在其中输入值,然后文本框背景更改为具有星形背景的文本框
- 尝试使用键盘上的后退键取消/删除输入值,然后文本框背景将更改为无星形背景的文本框
可绘制图形
:
然后,根据这种方法,您必须在EditText
上实现addTextChangedListener
方法。然后,作为参数,创建TextWatcher
类的新实例,并实现其方法:
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
及其方法onKey
:
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;
}
});
另一种方法:创建您自己的扩展类
参考资料:你吃过了吗嘿,谢谢,伙计们会检查你的links@KalpeshLakhani我所知道的唯一可行的方法就是定制EditText,在这里可以看到:@André.C.S感谢您的回复,但这在我的情况下不起作用,因为我想显示图像,而该链接显示了有关在edittext中自定义文本的内容。顺便说一句,谢谢你的朋友suggestion@KalpeshLakhani,没错,这是关于自定义文本:),这只是一种表示您肯定要做些什么的方式,以显示可绘制的提示!
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;
}
});
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
}
}
};