我有一个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"

如果有人想只使用数字从0到9 imeOptions启用,然后使用下面的行在您的EditText

android: inputType = " |没有数量"

这将只允许数字,如果你点击完成/键盘的下一个按钮,你的焦点将移动到下一个字段。

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

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

在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();
            }

在代码中,你可以这样做

ed_ins.setInputType(InputType.TYPE_CLASS_NUMBER);