How can restrict my EditText input to some special character like backslash(/),tild(~) etc by soft keyboard in android programmatically

前端 未结 10 1757
故里飘歌
故里飘歌 2020-12-08 04:02

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 programmatic

相关标签:
10条回答
  • 2020-12-08 04:14

    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.

    0 讨论(0)
  • 2020-12-08 04:19

    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
    0 讨论(0)
  • 2020-12-08 04:22

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

      android:digits="0123456789qwertzuiopasdfghjklyxcvbnm "
    
    0 讨论(0)
  • 2020-12-08 04:25

    Its late but may be helpfull for others. Instead of programaticaly, you can go with xml attribute. It may be the case that in Single layout you have many editText, out of which you wanna restrict special characters only in one EditText. So defining in xml will help you out. Here is the code to restrict special Characters by allowing them to only enter alphabets and numbers like below

    <EditText
         android:id="@+id/editText"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:textSize="@dimen/text10"
         android:singleLine="true"
         android:maxLines="1"
         android:maxLength="16"
         android:digits="abcdefghijklmnopqrstuvwxyz0123456789"/>
    
    0 讨论(0)
  • 2020-12-08 04:25

    you can prevent for typing special character:

    yourEditText.addTextChangedListener(new TextWatcher() {
          CharSequence previous;
          public void afterTextChanged(Editable s) {
            if(s.toString().contains("&^%$#*&(")){
                  s.clear();
                  s.append(previous.toString());
            }
          }
    
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {    
                previous = s;
          }
    
          public void onTextChanged(CharSequence s, int start, int before, int count) {}
       });
    
    0 讨论(0)
  • 2020-12-08 04:32

    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);
    
    0 讨论(0)
提交回复
热议问题