EditText input limit

Recently, in a project, if you need to input a name, it can only be Korean (Bangzi's app), without any numbers, special symbols, English letters...

I thought it would be easy to query the Korean Unicode range "\ uAC00-\uD7A3"

And then write a regular
"^[\uAC00-\uD7A3]+$"

Then write an EditText to inherit EditText

public class LimitEditText extends EditText {

    public LimitEditText(Context context) {
        super(context);
    }

    public LimitEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LimitEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * typewriting
     * @param outAttrs
     * @return
     */
    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        return new mInputConnecttion(super.onCreateInputConnection(outAttrs),
                false);
    }
}

class mInputConnecttion extends InputConnectionWrapper implements InputConnection {

    public mInputConnecttion(InputConnection target, boolean mutable) {
        super(target, mutable);
    }

    /**
     * Intercept input
     *
     * @param text
     * @param newCursorPosition
     * @return
     */
    @Override
    public boolean commitText(CharSequence text, int newCursorPosition) {
        // Only Chinese characters can be entered
        if (!text.toString().matches("^[\uAC00-\uD7A3]+$") || text.toString().matches("^[0-9]*$")) {
            return false;
        }
        return super.commitText(text, newCursorPosition);
    }

    @Override
    public boolean sendKeyEvent(KeyEvent event) {
        return super.sendKeyEvent(event);
    }

    @Override
    public boolean setSelection(int start, int end) {
        return super.setSelection(start, end);
    }
}

Think it's ok
The test is OK, but after using the go input method, everything is normal
But if you switch Arabic digital input in English, you can input the numbers into edittext
debug found that after cutting numbers in English, input numbers to enter

 @Override
    public boolean commitText(CharSequence text, int newCursorPosition) {
        // Only Chinese characters can be entered
        if (!text.toString().matches("^[\uAC00-\uD7A3]+$") || text.toString().matches("^[0-9]*$")) {
            return false;
        }
        return super.commitText(text, newCursorPosition);
    }

This method

No solution ~ ~ change method

Set a filter directly with native editText

userNameTxt.setFilters(new InputFilter[]{
                    new InputFilter() {
                        @Override
                        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                            String regex = "^[\uAC00-\uD7A3]+$";
                            boolean isKr = Pattern.matches(regex, source.toString());
                            if (!isKr) {
                                return "";
                            }
                            return null;
                        }
                    }
            });

In this way, passing the test can filter all rules.

If there is something wrong, please correct it.
End and scatter flowers~~

Posted on Tue, 19 May 2020 09:26:55 -0700 by daq