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条回答
唯我独甜
2楼-- · 2020-02-02 12:07

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) {}
   });
查看更多
Evening l夕情丶
3楼-- · 2020-02-02 12:11

Try this may works 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 });
    }

}
查看更多
老娘就宠你
4楼-- · 2020-02-02 12:12

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" />
查看更多
Viruses.
5楼-- · 2020-02-02 12:12

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"/>
查看更多
登录 后发表回答