【发布时间】:2013-09-02 22:54:41
【问题描述】:
我正在尝试实现EditText,它也将输入限制为仅限大写字符 [A-Z0-9] 和数字。
我从一些帖子开始使用 InputFilter 方法。但是在这里我在三星 Galaxy Tab 2 上遇到了一个问题,但在模拟器或 Nexus 4 中却没有。
问题是这样的:
- 当我输入“A”时,文本显示为“A”,这很好
- 现在当我输入“B”时,文本应该是“AB”,但它给了我“AAB” 这看起来很奇怪。
简而言之,它重复字符
这是我正在使用此代码的代码:
public class DemoFilter implements InputFilter {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
int dend) {
if (source.equals("")) { // for backspace
return source;
}
if (source.toString().matches("[a-zA-Z0-9 ]*")) // put your constraints
// here
{
return source.toString().toUpperCase();
}
return "";
}
}
XML 文件代码:
<EditText
android:id="@+id/et_licence_plate_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="0"
android:imeOptions="actionNext"
android:inputType="textNoSuggestions"
android:maxLength="3"
android:singleLine="true"
android:textSize="18px" >
</EditText>
我完全坚持这一点,所以这里的任何帮助将不胜感激。
【问题讨论】:
-
我尝试了所有其他解决方案并最终实现了它。看我的回答here
标签: android android-edittext android-input-filter