我有一个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:numeric="integer" 

虽然这也更自定义

android:digits="0123456789"

试试下面的代码:

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

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

例如:

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

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

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