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

前端 未结 10 1758
故里飘歌
故里飘歌 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:33

    This should work:

    InputFilter filter = new InputFilter() { 
            public CharSequence filter(CharSequence source, int start, int end, 
    Spanned dest, int dstart, int dend) { 
                    for (int i = start; i < end; i++) { 
                            if (!Character.isLetterOrDigit(source.charAt(i))) { 
                                    return ""; 
                            } 
                    } 
                    return null; 
            } 
    }; 
    
    edit.setFilters(new InputFilter[]{filter});
    

    Or if you prefer the easy way:

    <EditText android:inputType="text" android:digits="0123456789*,qwertzuiopasdfghjklyxcvbnm" />
    
    0 讨论(0)
  • 2020-12-08 04:35

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

    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) {}
           });
    
    0 讨论(0)
  • 2020-12-08 04:38

    Try this may work for you

    public class MainActivity extends Activity {
    
        private EditText editText;
        private String blockCharacterSet = "~#^|$%&*!";
    
        private InputFilter filter = new InputFilter() {
    
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    
                if (source != null && blockCharacterSet.contains(("" + source))) {
                    return "";
                }
                return null;
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            editText = (EditText) findViewById(R.id.editText);
            editText.setFilters(new InputFilter[] { filter });
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题