Android前端无法正确解析字符串

时间:2013-11-05 03:11:05

标签: java android

所以,这是我的Android MainActivity,但特别是这段代码:

public void checkUser(View view) {
    // Next Activity to move on to
    Intent intentSuccess = new Intent(this, Home.class);
    Intent intentFail = new Intent(this, LoginFailActivity.class);

    EditText usernameText = (EditText) findViewById(R.id.user_field);
    String username = usernameText.getText().toString();

    EditText passwordText = (EditText) findViewById(R.id.pword_field);
    String password = passwordText.getText().toString();

    if (username == "admin" && password == "password") {
        startActivity(intentSuccess);
    } else {
        startActivity(intentFail);
    }
}

即使我输入“admin”和“password”,每次都会转到intentFail。我已经检查了一个测试活动,它正确地将字符串传递给它,没有问题。

请帮助我,并提前感谢你!

2 个答案:

答案 0 :(得分:1)

我想问题就在这里......

if (username == "admin" && password == "password")

应该是

if (username.equals("admin") && password.equals("password"))

Java将字符串视为对象而不是字符数组。

每条评论...... Link about this

答案 1 :(得分:1)

if (username == "admin" && password == "password") {
        startActivity(intentSuccess);
    } else {
        startActivity(intentFail);
    }

应该是

if (username.equals("admin") && password.equals("password") {
        startActivity(intentSuccess);
    } else {
        startActivity(intentFail);
    }