how to set only numeric value for edittext in andr

2019-01-03 01:50发布

I have an edittext in which I want only integer values to be inserted. Can somebody tell me which property I have to use?

11条回答
混吃等死
2楼-- · 2019-01-03 02:13
<EditText
android:id="@+id/age"
android:numeric="integer" 
/>
查看更多
爷、活的狠高调
3楼-- · 2019-01-03 02:16

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();
            }
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-03 02:20

I guess android:inputType="number" at the xml attributes.

查看更多
萌系小妹纸
5楼-- · 2019-01-03 02:22

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.

查看更多
虎瘦雄心在
6楼-- · 2019-01-03 02:25

the simplest for me

android:numeric="integer" 

although this also more customize

android:digits="0123456789"
查看更多
霸刀☆藐视天下
7楼-- · 2019-01-03 02:26

In code, you could do

ed_ins.setInputType(InputType.TYPE_CLASS_NUMBER);
查看更多
登录 后发表回答