为什么 `equals` 不比较字符串和字符串数组?

Why does `equals` not compare string and string array?

我正在比较来自 TextEdit 的输入与来自 "answerList" 的答案。现在我想知道:为什么 .equals() 不比较 "uinput" String?有人可以向我解释一下并在代码中使用它吗?

在此先致谢,祝您有愉快的一天!

package ...

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

public TextView view1;
public String uinput;
public EditText edit1;
public TextView score_view;
public int score = 0;

public String[] questionList = {
        "lux, luces",
        "munus, munera",
        "neglere",
};

public String[] answerList = {
        "(dag)licht, dag",
        "taak",
        "verwaarlozen",
};

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

    this.edit1 = findViewById(R.id.edit1);
    this.view1 = findViewById(R.id.view1);
    this.score_view = findViewById(R.id.score_view);
    this.uinput = edit1.getText().toString();
    view1.setText(questionList[0]);
}

    public void check(View view) {
        if (uinput.equals(answerList[0])) {
            edit1.setBackgroundColor(Color.parseColor("#00FF00"));
            score++;
            score_view.setText(score);
        } else {
            edit1.setBackgroundColor(Color.parseColor("#FF0000"));
        }
}

}

删除这个关键字

......
edit1 = findViewById(R.id.edit1);
view1 = findViewById(R.id.view1);
score_view = findViewById(R.id.score_view);

......

在 OnClick() 方法中添加这个

uinput = edit1.getText().toString();

OP 的问题涉及将 uinput 与数组 questionList 中的元素进行比较。在 check 方法中,比较是针对 uinput 执行的,但是 uinput 的值在检查之前没有被更新。

public void check(View view) {
    // ADD HERE: update the value of the input
    uinput = edit1.getText().toString();

    if (uinput.equals(answerList[0])) {
        edit1.setBackgroundColor(Color.parseColor("#00FF00"));
        score++;
        score_view.setText(score);
    } else {
        edit1.setBackgroundColor(Color.parseColor("#FF0000"));
    }
}