我有一个 EditText
,我只想插入整数值。 有人可以告诉我我需要使用哪个属性吗?
我有一个 EditText
,我只想插入整数值。 有人可以告诉我我需要使用哪个属性吗?
android:inputType="number"
添加为 XML 属性。android:inputType="phone"
代替它。但是这似乎有点过于繁琐。 - Chad Bingham<EditText
android:id="@+id/myNumber"
android:digits="0123456789."
android:inputType="numberDecimal"
/>
0123456789.
而不是 0123456780.
- audiFanatic在代码中,你可以这样做
ed_ins.setInputType(InputType.TYPE_CLASS_NUMBER);
如果只输入数字,请使用android:inputType="numberPassword"
,以及editText.setTransformationMethod(null);
来移除数字的自动隐藏。
或者
android:inputType="phone"
对于仅输入数字,我认为这几种方法比android:inputType="number"
更好。将"number"作为inputType的限制是键盘允许切换到字符,并且还可以输入其他特殊字符。"numberPassword" inputType没有这些问题,因为键盘只显示数字。即使"phone" inputType也可以正常工作,因为键盘不允许您切换到字符。但是您仍然可以输入一些特殊字符,例如+、/、N等。
android:inputType="numberPassword"与editText.setTransformationMethod(null);
inputType="phone"
inputType="number"
android:inputType="numberPassword"
与 editText.setTransformationMethod(null);
一起使用。 - coderpcandroid:inputType="numberDecimal"
<EditText
android:id="@+id/age"
android:numeric="integer"
/>
这将只允许输入数字,如果您点击键盘上的完成/下一个按钮,则焦点将移动到下一个字段。android:inputType="number|none"
Edittext_name.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
它将允许您仅输入数字,无法输入字符。
如果您想输入字符而不是数字,则可以在getInstance
内编辑引号之间的值。
<EditText
android:id="@+id/age"
android:inputType="numberDecimal|numberSigned" />
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();
}
你可以在XML中使用它
<EditText
android:id="@+id/myNumber"
android:digits="123"
android:inputType="number"
/>
或者,
android:inputType="numberPassword" along with editText.setTransformationMethod(null); to remove auto-hiding of the number.
或者,
android:inputType="phone"
在编程中,您可以使用
editText.setInputType(InputType.TYPE_CLASS_NUMBER);