java - Android If 语句不起作用

标签 java android if-statement

<分区>

我目前正在开发一款应用,要求用户输入密码才能访问游戏。我有以下 if 语句,但它不起作用,正如您从 if 语句中看到的那样,我尝试了三种不同的方法来使匹配等于 true。

    EditText passInput = (EditText) findViewById(R.id.passwordBox);
    CharSequence Password = passInput.getText();
    RelativeLayout loggedIn = (RelativeLayout) findViewById(R.id.LoggedInLayout);
    RelativeLayout CreateUser = (RelativeLayout) findViewById(R.id.createUserLayout);
    Button loginBtt = (Button) findViewById(R.id.createUser);
    String actualPass = password[x];

    System.out.println(Password + actualPass + passInput);

    if(Password.equals(actualPass)){

        System.out.println("They Matched!");
        loggedIn.setVisibility(View.VISIBLE);
        CreateUser.setVisibility(View.GONE);
        loginBtt.setText("Create User");

    }else if(Password.toString() == actualPass.toString()){

        System.out.println("Second Match");

    }else if(Password == actualPass){

        System.out.println("Third Match");

    }else if(Password.equals(actualPass) == false){

    System.out.println("Wrong");
    incorrectPassword();
    System.out.println(Password);
    System.out.println(actualPass);

    }

当用户注册时,他们需要设置密码。为了测试,我尝试了密码“trst”,但是当插入到登录页面时,它返回时显示不正确。这是我的 LogCat 显示的内容:

11-07 11:46:16.357: I/System.out(1998): Wrong
11-07 11:46:16.547: I/System.out(1998): trst
11-07 11:46:16.547: I/System.out(1998): trst

正如您从 LogCat 中看到的,插入的密码和实际密码是相同的,但程序说它们不是!

最佳答案

使用 .equals() 而不是 == 来检查一个 String 对象是否等于另一个。 == 返回 true 如果两个引用引用同一个对象,而 .equals() 返回 true 如果内容两个字符串的相同。

关于java - Android If 语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19835413/

相关文章:

Java语法 "... = () -> {...}"

android - 如何删除 Android CheckBox 周围的填充

android - 拉取与新闻文章相关的缩略图

javascript - 替代一百万个 IF 语句

flutter - Flutter:如何编写if-else语句来调用抽屉?

javascript - 压缩类似的 IF 语句

java ;试图建立一个接受多个客户端连接的服务器

java - 如何让我的玩家沿对角线和水平线移动

java - Eclipse 重构 : move method inside collaborator

android - 为 GridView 的每个按钮打开一个新 Activity