How can restrict my EditText input to some special

2020-02-02 11:22发布

I am developing an application for keyboard, but i am geting an issue. I want to restrict/block some special character from soft keyboard in EditText in android programmatically.

So, Is there any way i can restrict any special character input in edit text in android.

If anyone have idea,Please reply.

Thanks in advance.

10条回答
Evening l夕情丶
2楼-- · 2020-02-02 11:51

If you want to add spaces you can give space after the last digit.

  android:digits="0123456789qwertzuiopasdfghjklyxcvbnm "
查看更多
我只想做你的唯一
3楼-- · 2020-02-02 11:54

check this link which shows How to restrict special characters from an Android EditText field?

Try this code android:digits="abcde.....012345789" i guess this is the easiest way to do.Hope this help you.

查看更多
Rolldiameter
4楼-- · 2020-02-02 11:58

First need to add DigitsKeyListener for allowing characters and then setRawInputType to edittext field

edit_field.setKeyListener(DigitsKeyListener.getInstance("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));


edit_field.setRawInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
查看更多
闹够了就滚
5楼-- · 2020-02-02 12:00

You can create regular expression and check it on onTextChanged method

yourEditText.addTextChangedListener(new TextWatcher() {

          public void afterTextChanged(Editable s) {

            // you can call or do what you want with your EditText here
            yourEditText. ... 

          }

          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          public void onTextChanged(CharSequence s, int start, int before, int count) {}
       });
查看更多
家丑人穷心不美
6楼-- · 2020-02-02 12:01

Unfortunately the accepted solution doesn't work in all the cases. The proper solution would be to use the following InputFilter:

private InputFilter filter = new InputFilter() {
    // An example pattern that restricts the input only to the lowercase letters
    private static final Pattern restrictedChars = Pattern.compile("[a-z]*")

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        final CharSequence replacementText = source.subSequence(start, end);
        final CharSequence replacedText = dest.subSequence(dstart, dend);

        if (source == null || restrictedChars.matcher(replacementText).matches()) {
            return null; // Accept the original replacement
        }

        return replacedText; // Keep the original text
    }
};

This solution differs from the accepted one in that it solves the following problems:

  • only a subsequence of the source is the replacement, not the full source
  • source doesn't necessarily include only the newly typed text, sometimes it is the full text typed so-far
查看更多
欢心
7楼-- · 2020-02-02 12:06

For those who might be facing issues while adding space please add a blank space with all the alphabets. Below is an example Also you should know that user won't be able to add a new line in this case.

            <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:digits="0123456789,a bcdefghijklmnopqrstuvwxyz"
            android:maxLines="1"
            android:singleLine="true" />
查看更多
登录 后发表回答