Huo*_*eng 12 android android-edittext
我想在用户输入字符时自定义edittext,然后edittext将其更改为图像.看起来像图像:
注意:●图像不是符号.
Rav*_*avi 20
你需要扩展PasswordTransformationMethod和使用setTransformationMethod方法EditText.
edt.setTransformationMethod(new CustomPasswordTransformationMethod());
Run Code Online (Sandbox Code Playgroud)
并粘贴这个 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码将写入字符,因为它最多5个字符,之后它将打印子弹EditText.
参考这篇文章
UPDATE
最后这是你的答案:
Spannable.Factory spannableFactory;
int lastIndex = -1;
spannableFactory = Spannable.Factory
.getInstance();
Run Code Online (Sandbox Code Playgroud)
加入addTextChangedListener你的EditText.
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) {
}
};
Run Code Online (Sandbox Code Playgroud)
将您的drawable转换为 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;
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
1606 次 |
| 最近记录: |