【问题标题】:If condition is not evaluated如果未评估条件
【发布时间】:2016-09-29 08:11:18
【问题描述】:

运行时,不符合条件。
它跳过它并运行“else”块。

你能帮我吗?

public class LoginActivity extends AppCompatActivity {

private EditText usern;
private EditText passw;
private Button logButton;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    usern = (EditText) findViewById(R.id.userNameText);
    passw = (EditText) findViewById(R.id.passwordText);
    logButton = (Button) findViewById(R.id.loginButton);

    logButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(usern.getText().toString().equals("demo") && passw.getText().equals("demo")){
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "Login...", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(getApplicationContext(), "Username or password incorrect", Toast.LENGTH_SHORT).show();
            }
        }
    });
}
}

【问题讨论】:

  • 详细描述你的问题......什么是预期行为什么是实际行为等等......

标签: java android xml android-studio if-statement


【解决方案1】:

我猜这行有问题..你为 passw 遗漏了 toString()

if(usern.getText().toString().equals("demo") && passw.getText().toString().equals("demo")){
                    ....

而不是

if(usern.getText().toString().equals("demo") && passw.getText().equals("demo")){
                    .... 

【讨论】:

  • 很高兴它有帮助:)
【解决方案2】:

您忘记为密码 EditText 附加 toString() 方法。

passw.getText().toString().equals("demo"))

【讨论】:

    【解决方案3】:

    if(usern.getText().toString().equals("demo") && passw.getText().equals("demo"))

    请检查密码条件检查。你错过了 .toString() 。

    使用如下代码,

    if(usern.getText().toString().equals("demo") && passw.getText().toString().equals("demo"))

    【讨论】:

      猜你喜欢
      • 2019-09-23
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2011-08-04
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多