EditText which accepts only a few characters

夙愿已清 提交于 2019-12-13 13:24:08

问题


I need to implement an EditText on Android which accepts only a specific range of characters, which are: "S,A,Q,W,R,B,C,D,E,U".


回答1:


What about using these two attributes in your EditText

android:maxLength="1"
android:digits = "SABCDEU"



回答2:


Just use attribute android:maxLength="1", this will allow only one character input to edittext.

You can Use android:digits property and specify in the XML itself what are the valid characters for you.

 <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:digits = "SABCDEU"
        android:maxLength="1" >



回答3:


Yes as below:

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:digits="SABCDEU"
    android:maxLength="1" > 



回答4:


If you want to set the number of maximum characters edittext should accept then use this:

 android:maxLength="1" 

If you want to set specific characters edittext should accept then use this:

android:digits = "character you want to accept"

in your case:

android:digits = "SABCDEU"

here note that edittext will not accept numeric characters,space,lowercases and Uppercase except SABCDEU. You have to put all character in android:digits="" if you wish to enter to edittext.




回答5:


Just add this piece of code in your Java

 public class MainActivity extends Activity {

    private EditText editText;

    private String blockCharacterSet = "~#^|$%&*!FGHIJKLMOPTWXYZ";

    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 });
    }

}

This will give you what you want. This will let you select only "S,A,Q,W,R,B,C,D,E,U".




回答6:


You should use InputFilters

edittext.setFilters(new InputFilter[] {
new InputFilter() {
    public CharSequence filter(CharSequence src, int start,
            int end, Spanned dst, int dstart, int dend) {
        if(src.toString().matches("[a-zA-Z ]+")){
            return src;
        }
        return "";
    }
}});



回答7:


Use this code, it only allows characters

<EditText 
android:inputType="text" 
android:digits="SAQWRBCDEU"
android:hint="Only letters allowed" />


来源:https://stackoverflow.com/questions/36027077/edittext-which-accepts-only-a-few-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!