How to determine if an input in EditText is an integer?

后端 未结 7 2013
轻奢々
轻奢々 2021-02-12 12:48

Hi I\'m a newbie in Android Programming.

I\'m trying to build an activity which includes an edittext field and a button. When user type in an

7条回答
  •  你的背包
    2021-02-12 13:39

    Update:

    You can control the EditText to only accept numbers

    
    

    or check it programmatically

    In Kotlin

    val number = editText123.text.toString().toIntOrNull() 
    val isInteger = number != null
    

    In Java

    String text = editText123.getText().toString();
    try {
       int num = Integer.parseInt(text);
       Log.i("",num+" is a number");
    } catch (NumberFormatException e) {
       Log.i("",text+" is not a number");
    }
    

提交回复
热议问题