在应用程序中字段为空时吐司

Toast when fields are Empty in Application

我想在字段为空时显示 toast。

button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
        }

    });

而不是吐司,"setError" 编辑文本。会更好

editText.setError("This field can not be blank");

试试这个,

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(edittext1.getText().toString().length()<=0){
            Toast.makeText(this, "email is empty", Toast.LENGTH_SHORT).show();
        }else if (edittext2.getText().toString().length()<=0){
            Toast.makeText(this, "password is empty", Toast.LENGTH_SHORT).show();
        }else{
            aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
        }
    }

});

在那之前

aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener)

尝试检查数据是否为空如下

if (edittext1.getText().toString().isEmpty() || edittext2.getText().toString().isEmpty()) {
    Toast.makeText(this,"fill the required field",Toast.LENGTH_LONG).show();
} else {
    aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener)
}

试试这个:

当字段为空时,应用此检查:-

if (edittext1.getText().toString().isEmpty()) {

Toast.makeText(getApplicationContext(),"Edit text 1 can not be empty",Toast.LENGTH_LONG).show();

}

else if (edittext2.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(),"Edit text 2 can not be empty",Toast.LENGTH_LONG).show();
   }
    else{
     //success code here
        aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);

    }