how to toast a message if editText is empty by clicking button?

前端 未结 5 1066
暗喜
暗喜 2021-01-24 09:19

i have 2 edit Text in my application, 1 button to add the input numbers in the edit Text and 1 text view to display the result. I would like to put a toast message if my edit te

5条回答
  •  忘掉有多难
    2021-01-24 09:39

    You can try this two function for string empty, not empty or null. It is simple return true or false. It is very use for all projects.

    if(isEmpty(edittext.getText().toString())){
           // your Toast message if string is empty
    }
    
    
    if(isNotEmpty(edittext.getText().toString())){
          // your Toast message if string is not empty
    }
    
    
     public static boolean isEmpty(String str) {
    
            if (str == null)
                return true;
            else if (str.toString().trim().length() == 0)
                return true;
    
            return false;
        }
    
        public static boolean isNotEmpty(String str) {
    
            if (str == null)
                return false;
            else if (str.toString().trim().length() == 0)
                return false;
    
            return true;
        } 
    

提交回复
热议问题