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

前端 未结 5 990
暗喜
暗喜 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 10:00

    To create and show a Toast use this code:

    Toast.makeText(this, "Please input number", Toast.LENGTH_LONG).show();
    

    In order to work properly, you should replace this code:

    if (editText1.equals("")) {
        editText1.setError("please input number");
    }
    if (editText2.equals("")) {
        editText2.setError("please input number");
    }
    

    with this:

    if (editText1.getText().toString().length() == 0 || editText1.getText().toString().length() == 1) {
        Toast.makeText(this, "Please input number", Toast.LENGTH_LONG).show();
    }
    

提交回复
热议问题