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

后端 未结 7 2141
轻奢々
轻奢々 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:27

    You can use TextWatcher for EditText to get value of every change in EditText.You need to add interface of TextWatcher in your Activity.

     mEditText.addTextChangedListener(Your Class Name.this);
    

    on in method of TextWatcher

         @Override
    public void afterTextChanged(Editable s) {
        Log.v("Log_tag", "After TextChanged" + s.toString());
    
    }
    
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        Log.i("Log_tag", "Before TextChanged" + s.toString());
    
    }
    
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.e("Log_tag", "ontext Changed" + s.toString());
        //you can match here s with number or integer 
                 if(isNumeric( s.toString())){
                          //it is number     
                   }
        }
    
    
     public static boolean isNumeric(String str)
     {
        return str.matches("-?\\d+(.\\d+)?");
      }
    

提交回复
热议问题