简体   繁体   English

Android - 当我设置最大长度并且 android:imeOptions=“actionNext” 不显示在键盘上时,TextInputEditText 接受字母表产生问题

[英]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" 时不显示在键盘上

在此处输入图片说明

EDIT编辑

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();
        }
    }});

Input Filter with MaxLength具有 MaxLength 的输入过滤器

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.

相关问题 android:imeOptions =“ actionNext”不起作用 - android:imeOptions=“actionNext” not working android:imeOptions =“ actionNext”不会转到下一个EditText - android:imeOptions=“actionNext” does not go to the next EditText 在表格布局中使用 android:imeOptions=&quot;actionNext&quot; 时出现问题 - Problem using android:imeOptions=“actionNext” in Table Layout android:imeOptions=&quot;actionNext&quot; 在不指定 android:inputType 的情况下不起作用 - android:imeOptions=“actionNext” does not work without specifying android:inputType 带有imeoptions的Android隐藏键盘 - Android hiding keyboard with imeoptions 如何在ActionBarActivity的片段中处理android:imeOptions =“ actionNext”,“ actionDone”? - How to handle android:imeOptions=“actionNext”,“actionDone” in fragments in ActionBarActivity? android:imeOptions的问题 - Issue with android:imeOptions 如何将最大长度设置为android键盘 - How to set max length to android keyboard Android TextInputEditText 问题提示 - Android TextInputEditText issue with hint 使用两个不同但非常相似的布局会导致android:ImeOptions =“ ActionNext”无法正常工作 - Using two different, but very similar, layouts causes android:ImeOptions=“ActionNext” to not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM