我应用的主要用户安装了 2 种语言。英语等。默认系统语言不是英语。 用户只需使用软件键盘上的语言切换按钮即可。
我的应用程序中一个特定的 EditText 字段只需要接受 A-Z(大写问题不是问题)和空格,而不需要其他字符(没有数字、没有非拉丁字符等)。
我了解使用输入过滤器的解决方案,例如 How to create EditText accepts Alphabets only in android?或者使用 TextWatcher 派生但你只允许应用程序简单地忽略不正确的文本,我需要能够让用户甚至无法在他们的屏幕键盘上首先看到非 latin1 字母(我知道它是可以使用硬件键盘,目前这不是问题)。
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
也无济于事(语言更改按钮在键盘上仍然可见)。
我需要像 iPhone: Change Keyboard language programmatically 这样的东西但对于 Android。
除了向我的应用添加假键盘外,我还有其他选择吗?
iOS 有 .keyboardType = .asciiCapable
并且它在这种情况下工作
最佳答案
您可以使用输入过滤器并将其分配给编辑文本
引用下面的例子
public class AlphabetInputFilter implements InputFilter {
Pattern mPattern;
public AlphabetInputFilter() {
mPattern = Pattern.compile("[a-z]");
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String formatedSource = dest.subSequence(0, dstart).toString();
String destPrefix = source.subSequence(start, end).toString();
String destSuffix = dest.subSequence(dend, dest.length()).toString();
CharSequence match = TextUtils.concat(formatedSource, destPrefix, destSuffix);
Matcher matcher = mPattern.matcher(match);
if (!matcher.matches())
return "";
return null;
}
你可以给它分配编辑文本
mEdittext.setFilters(new InputFilter[]{
new AlphabetInputFilter()});
用户将无法输入除 a 到 z 以外的任何值
关于android - 只有 ascii 字母的 EditText。如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52661305/