如果一个Edittext为空而另一个不是,那么如何让android响应呢?

时间:2015-04-21 11:11:14

标签: java android

如果一个Edittext为空而另一个不是,那么如何让Android显示Toast?

到目前为止的代码

if (sPassword.matches(sPassword2)) {
    Context finished = getApplicationContext();
    CharSequence registered = "You have been Registered!";
    int duration = Toast.LENGTH_SHORT;

    Toast wrong = Toast.makeText(finished, registered, duration);
    wrong.show();

    startActivity(finish);
} else {
    nomatch = (TextView) findViewById(R.id.unmatched);
    nomatch.setText("Passwords do not match");
    nomatch.setTextColor(getResources().getColor(R.color.red));

    Context unmatched = getApplicationContext();
    CharSequence improper = "Passwords do not match";
    int duration = Toast.LENGTH_SHORT;
    Toast noMatch = Toast.makeText(unmatched, improper, duration);
    noMatch.show();
}

4 个答案:

答案 0 :(得分:1)

你要做的是

if (sPassword.equals("") && (!sPassword2.equals(""))) {

    // that means your sPassword edit text is empty and sPassword2 is not empty.
    // your code
}

答案 1 :(得分:0)

if (sPassword.gettext().tostring().trim().equals("") || sPassword2.gettext().tostring().trim().equals("")) {

    // It will enter here even if one of them is empty
}

答案 2 :(得分:0)

if(sPassword.getText().toString().isEmpty() || sPassword2.getText().toString().isEmpty()){
                Toast toast = Toast.makeText(getApplicationContext(), "ADD YOUR MESSAGE HERE", Toast.LENGTH_LONG);
                toast.show();
                return;
            }

答案 3 :(得分:0)

@Moubeen Farooq Warar补充说:你应该检查“”是否相同:

if("".equals(sPassword)&&(!"".equals(sPassword2))){

  // it will not couse NullPointerException
 }