Max Usanin
Max Usanin

Reputation: 2499

what "android: inputType" to choose?

I need to enter only the following characters 0-9 and _

What "android: inputType" in EditText to choose?

Upvotes: 1

Views: 233

Answers (4)

Rahul Baradia
Rahul Baradia

Reputation: 11961

If you want to enter only number then choose this android:digits="0123456789_".

Upvotes: 0

Priya
Priya

Reputation: 1803

ok.try the following code

   InputFilter[] Textfilters = new InputFilter[1];
    Textfilters[0] = new InputFilter(){
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        // TODO Auto-generated method stub
        if (end > start) {

            char[] acceptedChars = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_'};

            for (int index = start; index < end; index++) {
                if (!new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) {
                return "";
                  }
                }
             }
                return null;
        }

    };
 editxt.setFilters(Textfilters);

Upvotes: 0

Siddhesh
Siddhesh

Reputation: 1380

just do this in XML

android:digits="0123456789_"

Upvotes: 1

KEYSAN
KEYSAN

Reputation: 885

Try and add this to your EditText

EditText text = findViewById(R.id.your_edit_text); /* Find your EditText here. */
text.setKeyListener(DigitsKeyListener.getInstance("0123456789_"));

Upvotes: 0

Related Questions