Check if an Edit Text that only accepts number is not empty and the number is equal or less than 100

前端 未结 5 1327
清歌不尽
清歌不尽 2020-12-20 07:57

I\'m building an application for receiving grades and I want to make sure that the Edit Texts are not empty and the values are less or equal to 100 I wrote this line but it

5条回答
  •  一整个雨季
    2020-12-20 08:37

    If you want to control what digit should enter use android:digits=""

    android:inputType="number" enforce to open number type keyboard still user can press other char like "#,." than may cause NumberFormatException

    
    

    android:digits="0123456789" only accepts 0123456789 in EditText, not any other char

    Before converting any string integer you must check if it is empty

    String strNumber=editText.getText().toString().trim();
     if(TextUtils.isEmpty(strNumber) || Integer.parseInt(strNumber)>100){
       // show your error message
    }
    

提交回复
热议问题