简体   繁体   中英

Android EditText force numeric keyboard but allow non-numeric chars

I need to maintain an EditText which can be inputted by the user numbers, at some cases I need to set the text of the EditText to something like 12$. So I need to be able to setText() to anything I want.

I've tried setting up an EditText with input type set to number which yields the numeric keypad and also remove the InputFilter 's of that EditText - Which still does not allow me to put non numeric chars

input.setFilters(new InputFilter[] {});

Am I doing something wrong? Is there a different way to accomplish my goal, an EditText with a numeric keypad that i can programmatically insert some non numeric chars into?

This will keep it numeric but will allow the $ character, add any other special chars you need to the android:digits attribute

<EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:digits="0123456789,$">

Just to add to aviran's answer. If you put any "," in the string then you will also allow commas in the EditText.

Below will only allow 0-9 and $.

<EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:digits="0123456789$">

Set input type Phone in xml.

This could help you...

how to set only numeric value for edittext in android?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM