[英]Android - TextInputEditText accepts Alphabets produce issue when I set max length and android:imeOptions=“actionNext” not show on Keyboard
I have take TextInputEditText for first name only accepts alphabets but i have got problem.我已经将 TextInputEditText 作为名字只接受字母,但我遇到了问题。
<android.support.design.widget.TextInputLayout
android:id="@+id/tvfnameinput"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="@+id/etFirstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
android:hint="firstname"
android:imeOptions="actionNext"
android:maxLength="12"
android:maxLines="1"
android:text="" />
</android.support.design.widget.TextInputLayout>
As you see in above layout i have set maxlength 12 so if i have enter 12 numeric value then it's not accepts but it's count so i can't able to enter alphabets after entering 12 numeric value.正如您在上面的布局中看到的,我已将 maxlength 设置为 12,因此如果我输入了 12 个数值,那么它不会被接受,但它是计数,因此在输入 12 个数值后我无法输入字母。
NOTE -: I want just alphabets and my code is working but when i entered numeric value then it's not accept but count as a maxlenght.注意- :我只想要字母并且我的代码正在工作,但是当我输入数值时,它不被接受,但被视为最大长度。
Edit -: android:imeOptions="actionNext" not show on Keyboard when i have set android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"编辑-: android:imeOptions="actionNext" 当我设置 android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 时不显示在键盘上
If you want to set only Alphabets then you can use InputFilter instead of digits.如果您只想设置字母表,那么您可以使用 InputFilter 而不是数字。 Remove this tag
digit
from your layout.从您的布局中删除此标记
digit
。
Here is the example that can help you:以下是可以帮助您的示例:
edittext.setFilters(new InputFilter[] {
new InputFilter() {
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend) {
if(src.equals("")){ // for backspace
return src;
}
if(src.toString().matches("[a-zA-Z ]+")){
return src;
}
return "";
}
}
});
Try this one.试试这个。 it is tested by myself and working like charm
它经过我自己的测试并且像魅力一样工作
editText.setFilters(new InputFilter[]{new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
boolean keepOriginal = true;
StringBuilder sb = new StringBuilder(end - start);
for (int i = start; i < end; i++) {
char c = source.charAt(i);
if (isCharAllowed(c)) // put your condition here
sb.append(c);
else
keepOriginal = false;
}
if (keepOriginal)
return null;
else {
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(sb);
TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, sp, 0);
return sp;
} else {
return sb;
}
}
}
private boolean isCharAllowed(char c) {
Pattern ps = Pattern.compile("^[a-zA-Z ]+$");
Matcher ms = ps.matcher(String.valueOf(c));
return ms.matches();
}
}});
When using InputFilter the property of EditText is overridden then you can use max length with InputFiler.使用 InputFilter 时,EditText 的属性被覆盖,然后您可以使用 InputFiler 的最大长度。
Try this试试这个
InputFilter[] inputFilters = new InputFilter[2];
inputFilters[0] = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
boolean keepOriginal = true;
StringBuilder sb = new StringBuilder(end - start);
for (int i = start; i < end; i++) {
char c = source.charAt(i);
if (isCharAllowed(c)) // put your condition here
sb.append(c);
else
keepOriginal = false;
}
if (keepOriginal)
return null;
else {
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(sb);
TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, sp, 0);
return sp;
} else {
return sb;
}
}
}
private boolean isCharAllowed(char c) {
Pattern ps = Pattern.compile("^[a-zA-Z ]+$");
Matcher ms = ps.matcher(String.valueOf(c));
return ms.matches();
}
};
inputFilters[1] = new InputFilter.LengthFilter(12);
editText.setFilters(inputFilters);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.