Android: Limiting EditText to numbers

2019-01-27 14:07发布

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"/>

4条回答
姐就是有狂的资本
2楼-- · 2019-01-27 14:52

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

查看更多
萌系小妹纸
3楼-- · 2019-01-27 14:56

this will usually do it:

android:numeric="integer"

查看更多
迷人小祖宗
4楼-- · 2019-01-27 14:57

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.

查看更多
女痞
5楼-- · 2019-01-27 15:00

Something like this perhaps ?

EditText text = new EditText(this);
text.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
查看更多
登录 后发表回答