gpt4 book ai didi

android - EditText 和 InputFilter 导致重复文本

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:44:43 31 4
gpt4 key购买 nike

我正在尝试实现一个 EditText,它将输入限制为仅字母字符 [A-Za-z]。

我从 this post 中的 InputFilter 方法开始.当我键入“a%”时,文本消失,然后如果我按退格键,文本就是“a”。我尝试了过滤器功能的其他变体,例如使用正则表达式仅匹配 [A-Za-z],有时会看到疯狂的行为,例如重复字符,我将输入“a”,然后输入“b”,然后输入“aab”输入“c”并得到“aabaabc”,然后按退格键得到“aabaabcaabaabc”!

这是我目前使用的代码,使用了我尝试过的不同方法。

    EditText input = (EditText)findViewById( R.id.inputText );
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter( CharSequence source, int start, int end, Spanned dest, int dstart, int dend ) {
//String data = source.toString();
//String ret = null;
/*
boolean isValid = data.matches( "[A-Za-z]" );
if( isValid ) {
ret = null;
}
else {
ret = data.replaceAll( "[@#$%^&*]", "" );
}
*/
/*
dest = new SpannableStringBuilder();
ret = data.replaceAll( "[@#$%^&*]", "" );
return ret;
*/

for( int i = start; i < end; i++ ) {
if( !Character.isLetter( source.charAt( i ) ) ) {
return "";
}
}

return null;
}
};
input.setFilters( new InputFilter[]{ filter } );

我完全被这个问题难住了,所以这里的任何帮助都将不胜感激。

编辑:好的,我已经对 InputFilter 进行了大量试验并得出了一些结论,尽管没有解决问题的方法。请参阅下面我的代码中的注释。我现在要尝试 Imran Rana 的解决方案。

    EditText input = (EditText)findViewById( R.id.inputText );
InputFilter filter = new InputFilter() {
// It is not clear what this function should return!
// Docs say return null to allow the new char(s) and return "" to disallow
// but the behavior when returning "" is inconsistent.
//
// The source parameter is a SpannableStringBuilder if 1 char is entered but it
// equals the whole string from the EditText.
// If more than one char is entered (as is the case with some keyboards that auto insert
// a space after certain chars) then the source param is a CharSequence and equals only
// the new chars.
@Override
public CharSequence filter( CharSequence source, int start, int end, Spanned dest, int dstart, int dend ) {
String data = source.toString().substring( start, end );
String retData = null;

boolean isValid = data.matches( "[A-Za-z]+" );
if( !isValid ) {
if( source instanceof SpannableStringBuilder ) {
// This works until the next char is evaluated then you get repeats
// (Enter "a" then "^" gives "a". Then enter "b" gives "aab")
retData = data.replaceAll( "[@#$%^&*']", "" );
// If I instead always returns an empty string here then the EditText is blanked.
// (Enter "a" then "^" gives "")
//retData = "";
}
else { // source is instanceof CharSequence
// We only get here if more than 1 char was entered (like "& ").
// And again, this works until the next char is evaluated then you get repeats
// (Enter "a" then "& " gives "a". Then enter "b" gives "aab")
retData = "";
}
}

return retData;
}
};
input.setFilters( new InputFilter[]{ filter } );

最佳答案

使用以下代码:

EditText input = (EditText) findViewById(R.id.inputText);
input.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
for( int i = start;i<s.toString().length(); i++ ) {
if( !Character.isLetter(s.charAt( i ) ) ) {
input.setText("");
}
}

}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}
});

如果您希望有效文本保留在 EditText 中:


 input.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub

}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
for( int i = 0;i<s.toString().length(); i++ ) {
if( !Character.isLetter(s.charAt( i ) ) ) {
s.replace(i, i+1,"");
}
}
}
});

关于android - EditText 和 InputFilter 导致重复文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10792051/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com