如何自定义给定图中显示的 editext 的输入类型。基本上我的要求是 edittext 应该只显示最后 3 位或 4 位数字,只有最初的 12 位数字应该处于密码模式。
问问题
124 次
3 回答
0
您需要添加一个TextWatcher
到EditText
:
int characterCount = 0;
int asteriskCount = 0;
CharSequence input = null;
input.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
characterCount = count;
//update input sequence based on changes.
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//update input sequence based on changes.
}
@Override
public void afterTextChanged(Editable s) {
if (asteriskCount != characterCount) {
//make the visible sequence here.
CharSequence seq = "";
for (int i = 0; i < (characterCount <= 12 ? characterCount : 12); i++) {
seq = seq + "*";
}
if (characterCount > 12) {
for (int i = 12; i < characterCount; i++) {
seq = seq + characterCount.charAt(i);
}
}
asteriskCount = characterCount;
input.setText(seq);
}
}
});
于 2013-04-16T17:27:24.900 回答
0
没有这样的内置功能。所以你必须自己做。更改文本时,您必须对文本进行更改。这样做。
如果您通过扩展创建自定义 editText,EditText
则可以覆盖onTextChanged方法并处理更改。
或者您可以使用TextWatcher来处理更改。
因此,当文本更改时,将除最后 3 位以外的数据设置为 *。
但请记住,您必须使用 String 字段将原始数据存储在字段中。
于 2013-04-16T17:11:39.047 回答
-2
下面是我的 TextWatcher 的代码片段:
private boolean spaceDeleted;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
characterCount = start;
//update input sequence based on changes.
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// check if a space was deleted
CharSequence charDeleted = s.subSequence(start, start + count);
spaceDeleted = " ".equals(charDeleted.toString());
}
@Override
public void afterTextChanged(Editable s) {
if(s.length()>12){
return;
}
System.out.println("Character Count in afterTextChange->"+characterCount);
System.out.println("Editable Character->"+s);
ccNumber.removeTextChangedListener(this);
// record cursor position as setting the text in the textview
// places the cursor at the end
int cursorPosition = ccNumber.getSelectionStart();
String withSpaces = formatText(s);
ccNumber.setText(withSpaces);
// set the cursor at the last position + the spaces added since the
// space are always added before the cursor
ccNumber.setSelection(cursorPosition + (withSpaces.length() - s.length()));
// if a space was deleted also deleted just move the cursor
// before the space
if (spaceDeleted) {
ccNumber.setSelection(ccNumber.getSelectionStart() - 1);
spaceDeleted = false;
}
// enable text watcher
ccNumber.addTextChangedListener(this);
}
private String formatText(CharSequence s) {
// TODO Auto-generated method stub
StringBuilder formatted = new StringBuilder();
int count = 0;
/* if(s.length()<12){
formatted.append("*");
}else{
formatted.append(s.charAt(characterCount));
}*/
for (int i = 0; i < s.length(); ++i)
{
formatted.append("*");
/*if (Character.isDigit(s.charAt(i)))
{
if (count % 4 == 0 && count > 0)
formatted.append(" ");
formatted.append(s.charAt(i));
++count;
}*/
}
return formatted.toString();
}
});
于 2013-06-07T13:02:22.973 回答