Android: Limiting EditText to numbers

情到浓时终转凉″ 提交于 2019-11-28 07:30:14

问题


When creating an EditText in the java portion of the application, how do you limit it to numbers like you would in the xml? For example:

new EditText(this);

set like

<EditText
    android:id="@+id/h1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"/>

回答1:


Something like this perhaps ?

EditText text = new EditText(this);
text.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);



回答2:


Use of android:numeric is deprecated, use android:inputType=number




回答3:


Go to the docs and look at the table with XML attributes. It shows you the code equivalent of each item.

In this case, it's setRawInputType.




回答4:


this will usually do it:

android:numeric="integer"




回答5:


Simply use the below lines in the xml file

To accept only Numbers put:

 android:inputType="number"

To limit the length of the input numbers:

android:maxLength="10"

To accept only specific numbers:

EX:1 The below line accept only 1 or 0.

android:digits="10"

EX:2 The below line accept only 2 or 3.

android:digits="23"


来源:https://stackoverflow.com/questions/5200689/android-limiting-edittext-to-numbers

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