Android限制EditText只能输入中文、英文、数字

private EditText name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    name = findViewById(R.id.loginname);
    name.setFilters(new InputFilter[]{
            new InputFilter() {
                @Override
                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))
                                && !Character.toString(source.charAt(i)) .equals("_")
                                && !Character.toString(source.charAt(i)) .equals("-"))
                        {
                            return "";
                        }
                    }
                    return null;
                }
            }
    });
}

你可能感兴趣的:(Android)