how to set only numeric value for edittext in andr

2019-01-03 01:50发布

I have an edittext in which I want only integer values to be inserted. Can somebody tell me which property I have to use?

11条回答
小情绪 Triste *
2楼-- · 2019-01-03 02:27
android:inputType="numberDecimal"
查看更多
【Aperson】
3楼-- · 2019-01-03 02:28

You can use it in XML

<EditText
 android:id="@+id/myNumber"
 android:digits="123"
 android:inputType="number"
/>

or,

android:inputType="numberPassword" along with editText.setTransformationMethod(null); to remove auto-hiding of the number.

or,

android:inputType="phone"

Programmatically you can use
editText.setInputType(InputType.TYPE_CLASS_NUMBER);

查看更多
Animai°情兽
4楼-- · 2019-01-03 02:32

For example:

<EditText
android:id="@+id/myNumber"
android:digits="0123456789."
android:inputType="numberDecimal"
/>
查看更多
等我变得足够好
5楼-- · 2019-01-03 02:34

I need to catch pressing Enter on a keyboard with TextWatcher. But I found out that all numeric keyboards android:inputType="number" or "numberDecimal" or "numberPassword" e.t.c. don't allow me to catch Enter when user press it.

I tried android:digits="0123456789\n" and all numeric keyboards started to work with Enter and TextWatcher.

So my way is:

android:digits="0123456789\n"
android:inputType="numberPassword"

plus editText.setTransformationMethod(null)

Thanks to barmaley and abhiank.

查看更多
Animai°情兽
6楼-- · 2019-01-03 02:36

For only digits input use android:inputType="numberPassword" along with editText.setTransformationMethod(null); to remove auto-hiding of the number.

OR

android:inputType="phone"

For only digits input, I feel these couple ways are better than android:inputType="number". The limitation of mentioning "number" as inputType is that the keyboard allows to switch over to characters and also lets other special characters be entered. "numberPassword" inputType doesn't have those issues as the keyboard only shows digits. Even "phone" inputType works as the keyboard doesnt allow you to switch over to characters. But you can still enter couple special characters like +, /, N, etc.

android:inputType="numberPassword" with editText.setTransformationMethod(null);

inputType="numberPassword"

inputType="phone"

inputType="phone"

inputType="number"

inputType="number"

查看更多
登录 后发表回答