问题
I have an edittext in which I want only integer values to be inserted. Can somebody tell me which property I have to use?
回答1:
I guess android:inputType="number" at the xml attributes.
回答2:
For example:
<EditText
android:id="@+id/myNumber"
android:digits="0123456789."
android:inputType="numberDecimal"
/>
回答3:
In code, you could do
ed_ins.setInputType(InputType.TYPE_CLASS_NUMBER);
回答4:
For only digits input use android:inputType="numberPassword" along with editText.setTransformationMethod(null); to remove auto-hiding of the number.
OR
android:inputType="phone"
For only digits input, I feel these couple ways are better than android:inputType="number". The limitation of mentioning "number" as inputType is that the keyboard allows to switch over to characters and also lets other special characters be entered. "numberPassword" inputType doesn't have those issues as the keyboard only shows digits. Even "phone" inputType works as the keyboard doesnt allow you to switch over to characters. But you can still enter couple special characters like +, /, N, etc.
android:inputType="numberPassword" with editText.setTransformationMethod(null);
inputType="phone"
inputType="number"
回答5:
android:inputType="numberDecimal"
回答6:
<EditText
android:id="@+id/age"
android:numeric="integer"
/>
回答7:
using the below can solve your problem better;
in xml:
<EditText
android:id="@+id/age"
android:inputType="numberDecimal|numberSigned" />
or //in activity inside etfield.addtextchangelistener
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();
}
回答8:
If anyone want to use only number from 0 to 9 with imeOptions enable then use below line in your EditText
android:inputType="number|none"
This will only allow number and if you click on done/next button of keyboard your focus will move to next field.
回答9:
You can use it in 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"
Programmatically you can use
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
回答10:
I need to catch pressing Enter on a keyboard with TextWatcher. But I found out that all numeric keyboards android:inputType="number" or "numberDecimal" or "numberPassword" e.t.c. don't allow me to catch Enter when user press it.
I tried android:digits="0123456789\n" and all numeric keyboards started to work with Enter and TextWatcher.
So my way is:
android:digits="0123456789\n"
android:inputType="numberPassword"
plus editText.setTransformationMethod(null)
Thanks to barmaley and abhiank.
回答11:
the simplest for me
android:numeric="integer"
although this also more customize
android:digits="0123456789"
来源:https://stackoverflow.com/questions/4645119/how-to-set-only-numeric-value-for-edittext-in-android