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

前端 未结 10 1756
故里飘歌
故里飘歌 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: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 });
        }
    
    }
    

提交回复
热议问题