javaandroidregexandroid-edittext

Setting EditText imeOptions to actionNext has no effect when using digits


Here my edittext:-

    <com.os.fastlap.util.customclass.EditTextPlayRegular
                    android:id="@+id/full_name_et"
                    style="@style/Edittext_white_13sp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_5sdp"
                    android:background="#00ffffff"
                    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                    android:imeOptions="actionNext"
                    android:inputType="text"
                    android:maxLength="20"
                    android:maxLines="1"
                    android:nextFocusDown="@+id/last_name_et"
                    android:textCursorDrawable="@null" /> 

When I remove digit in edittext it work fine but with digit imeOptions doesn't work. But one surprising thing if I use singleLine instead of maxLines it work fine. But singleLine now is deprecated. I cannot remove digit in my edittext and I don't want use deprecated method. Any one can solve this problem. Thanks in adavance


Solution

  • Here is a simplified solution with the software keyboard button "Next":

    final String NOT_ALLOWED_CHARS = "[^a-zA-Z0-9]+";
    
    final EditText editText = (EditText) findViewById(R.id.editText);
    editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
    
            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s)) {
                    // remove the listener to avoid StackoverflowException
                    editText.removeTextChangedListener(this);
                    // replace not allowed characters with empty strings
                    editText.setText(s.toString().replaceAll(NOT_ALLOWED_CHARS, ""));
                    // setting selection moves the cursor at the end of string
                    editText.setSelection(editText.getText().length());
                    // add the listener to keep watching
                    editText.addTextChangedListener(this);
                }
            }
        });
    

    Here the regular expression [^a-zA-Z0-9]+ corresponds to the allowed values of android:digits of the EditText in question.