我有一个EditText,我只希望在其中插入整数值。谁能告诉我该用哪个属性?


当前回答

我需要捕捉按下键盘上的输入与TextWatcher。但我发现所有的数字键盘android:inputType="number"或"numberDecimal"或"numberPassword"等不允许我捕捉进入时,用户按下它。

我尝试了android:digits="0123456789\n",所有的数字键盘都开始使用Enter和TextWatcher。

所以我的方法是:

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

加上editText.setTransformationMethod(空)

多亏了barmaley和abhiank。

其他回答

添加android:inputType="number"作为XML属性。

试试下面的代码:

Edittext_name.setKeyListener(DigitsKeyListener.getInstance("0123456789"));

它只允许您输入数字。不能输入字符。 如果您想输入字符,而不是数字,您可以编辑getInstance内引号之间的值。

对于只有数字输入使用android:inputType="numberPassword"连同editText.setTransformationMethod(null);删除号码的自动隐藏。

OR

android: inputType =“电话”

对于只有数字输入,我觉得这两个方法比android更好:inputType="number"。将“number”作为inputType的限制在于,键盘允许切换到字符,也允许输入其他特殊字符。“numberPassword”inputType没有这些问题,因为键盘只显示数字。即使是“phone”inputType也可以工作,因为键盘不允许你切换到字符。但你仍然可以输入一些特殊字符,如+、/、N等。

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

inputType =“电话”

inputType =“当家”

<EditText
android:id="@+id/age"
android:numeric="integer" 
/>

您可以在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"

您可以通过编程方式使用 editText.setInputType (InputType.TYPE_CLASS_NUMBER);