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


当前回答

使用下面的方法可以更好地解决你的问题;

在xml:

<EditText
      android:id="@+id/age"
      android:inputType="numberDecimal|numberSigned"        />

或者//在etfield.addtextchangelistener的activity中

     private String blockCharacterSet="+(/)N,*;#";//declare globally
               try {
                for (int i = 0; i < s.length(); i++) {


                    if (blockCharacterSet.contains(s.charAt(i) + "")) {

                        String corrected_settempvalue = arrivalsettemp.substring(0, arrivalsettemp.length() - 1);
                        et_ArrivalSetTemp.setText(corrected_settempvalue);
                        if (corrected_settempvalue.length() != 0)
                            et_ArrivalSetTemp.setSelection(corrected_settempvalue.length());

                    }
                }
            } catch (Exception d) {
                d.printStackTrace();
            }

其他回答

android:inputType="numberDecimal"

对于只有数字输入使用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 =“当家”

我不知道13年的正确答案是什么,但今天是

myEditText.setKeyListener (DigitsKeyListener。getInstance(null, false, true));//正十进制数

你得到的一切,包括屏幕上的键盘是一个数字键盘。

几乎一切。Espresso以其无限的智慧,让typeText(“…”)莫名其妙地绕过过滤器进入垃圾…

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

例如:

<EditText
android:id="@+id/myNumber"
android:digits="0123456789."
android:inputType="numberDecimal"
/>