- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试实现一个 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
}
});
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/
在某些 Android 设备上,我在 InputFilter 内遇到了 IndexOutOfBoundsException,我用它来删除输入开头的任何零: 我的代码: zeroFilter =
我正在使用 InputFilter 类制作一个支持数字分组的屏蔽 EditText。例如,当用户插入“12345”时,我想在 EditText 中显示“12,345”。我该如何实现? 这是我的不完整代
我为 EditText 组件创建了一个 InputFilter,它只允许在一个范围内(例如从 1.5 到 5.5)出现 double 值。一切正常,直到我删除了小数点: 我输入了 1.68,然后删除了
我正在寻找 Jetpack Compose TextField 中 EditText 的 InputFilter 的等效方法。 因为我试图阻止用户输入不需要的值,例如 %@*()-以字符为例。 最佳答
$app->post('/', function () use ($app) { $email = new Input('email'); $email->getValidatorCh
我在这样的表单条目上有一个验证器: $this->add(array( 'name' => 'email', 'required' => true,
$app->post('/', function () use ($app) { $email = new Input('email'); $email->getValidatorCh
我在这样的表单条目上有一个验证器: $this->add(array( 'name' => 'email', 'required' => true,
到目前为止,我一直在将输入过滤器绑定(bind)到模块中的表单,换句话说,我一直在表单中创建元素,将输入过滤器添加到模块端的元素。 例如检查这个example 现在我正在根据需要动态创建文本字段元素,
在我的 ZF2 应用程序中,我有一个包含多个可选字段的表单。如果用户决定将该字段留空,那么它应该在数据库中将其设置为 NULL,但是 - 它不起作用。 示例字段: $this->add(array(
我想将 InputFilter 应用于我的 EditTextPreferences... 在我使用 PreferenceActivity 之前,我有 EditTexts 和这样的过滤器:
int maxLength = 20; private String blockCharacterSet = "~#^|$%'&*!;"; private InputFilter filter = n
我正在尝试实现一个 EditText,它将输入限制为仅字母字符 [A-Za-z]。 我从 this post 中的 InputFilter 方法开始.当我键入“a%”时,文本消失,然后如果我按退格键,
我正在尝试实现一个 EditText,它将输入限制为仅包含数字的大写字符 [A-Z0-9]。 我从一些帖子的 InputFilter 方法开始。但在这里我在 Samsung Galaxy Tab 2
street = (EditText) findViewById(R.id.street); InputFilter filter = new InputFilter() { public C
假设我有一个 EditText,我想将它限制为 0-9 和 A-F(含),用于十六进制输入。有什么办法可以做到吗?我试着查看 InputFilters,但我不确定这是正确的方法。有人做到了吗? 最佳答
是否有可能有 InputFilters 列表,例如只允许输入,例如$1,01 或 $100,95 已完成 editText.filters = arrayOf(CurrencyFormatInputF
我正在尝试自定义默认错误消息“Value is required and can't be empty” 在 zf2 我正在使用以下代码在 inputfilter 的验证器中添加自定义默认错误消息 $
所以我得到了一个 ZF2 应用程序,在我拥有的 InputFilter 中得到了一个 Form 和一个 InputFilter: $this->add( array(
我在编写的应用程序中遇到了一个非常奇怪的问题,即 EditText 分配了 InputFilter。这个想法是,拥有一个 EditText,根据用户的区域设置,它只接受十进制数字作为输入,最多两位小数
我是一名优秀的程序员,十分优秀!