你需要扩展 PasswordTransformationMethod
并使用 EditText
的 setTransformationMethod
方法。
edt.setTransformationMethod(new CustomPasswordTransformationMethod());
并粘贴此 CustomPasswordTransformationMethod
class CustomPasswordTransformationMethod extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence source;
public PasswordCharSequence(CharSequence source) {
this.source = source;
}
public char charAt(int index) {
if(index>4) //your own condition, when you want to hide characters.
return 0x2022; // change this to bullets you want like '*' or '.'
return source.charAt(index);
}
public int length() {
return source.length();
}
public CharSequence subSequence(int start, int end) {
return source.subSequence(start, end);
}
}
}
上述代码将原样写入字符,直到5个字符,之后它将在EditText
中打印出点。
参考自这篇文章
更新
最终这就是你的答案:
Spannable.Factory spannableFactory;
int lastIndex = -1;
spannableFactory = Spannable.Factory
.getInstance();
1. 在你的 EditText
中添加 addTextChangedListener
。
mEditText.addTextChangedListener(watcher);
TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (start>4) {
mEditText.removeTextChangedListener(watcher);
mEditText.setText(getIconText(context, s, start));
mEditText.addTextChangedListener(watcher);
mEditText.setSelection(s.length());
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
将您的可绘制对象转换为 Spannable
public Spannable getIconText(Context context, CharSequence text, int index) {
Spannable spannable = spannableFactory.newSpannable(text);
if (index>lastIndex) {
spannable.setSpan(new ImageSpan(context, R.drawable.bullet_point),
index, index + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
lastIndex=index;
return spannable;
}
假设您想用字符●替换字符b。那么您可以添加如下的TextWatcher:
myEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
StringBuilder myText = new StringBuilder(myEditText.getText().toString());
if (myText.toString().contains("b")){ //If this contains b
myText.setCharAt(myText.indexOf("b"),'●');
myEditText.setText(myText.toString()); //Sets the string to EditText
myEditText.setSelection(myText.length()); //Moves cursor to the last after replacing
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
更新
我们可以使用setCompoundDrawablesWithIntrinsicBounds来放置图片,但无法将其放置在精确位置作为字符。我们只能定义边界。
setCompoundDrawablesWithIntrinsicBounds
来实现,但无法将其放置在精确的位置上。我们只能定义边界。 - Shree Krishnaandroid:inputType="textPassword"
在你的代码中尝试使用这个功能。 文档
EditText text = (EditText) findViewById(R.id.edtTest);
text.setCompoundDrawablesWithIntrinsicBounds(null, null,
getResources().getDrawable(R.drawable.myDrawable), null);