比较TextView String时,equals方法不起作用

时间:2016-04-14 19:08:28

标签: java android android-studio textview equals

所以我是java的新手,我正在尝试将测验应用作为练习。我有点做了一个,但它不起作用:

public class QuizActivity extends AppCompatActivity {
TextView QuestionText;
Button button1;
Button button2;
Button button3;
Button button4;
ArrayList<Question> listOfQuestions;
int currentQuestion = 0;
Context context = this;
int NumberOfQuestions;
GameCreator game;
String totalCorrect = "";
String totalWrong = "";

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

    QuestionText = (TextView) findViewById(R.id.textJautajums);
    button1 = (Button) findViewById(R.id.buttonOpcija1);
    button2 = (Button) findViewById(R.id.buttonOpcija2);
    button3 = (Button) findViewById(R.id.buttonOpcija3);
    button4 = (Button) findViewById(R.id.buttonOpcija4);
    NumberOfQuestions = Integer.parseInt(context.getString(R.string.JautajumuSkaits).toString());
    game = new GameCreator(NumberOfQuestions);
    listOfQuestions = game.makeQuestions();

    Resources r = getResources();
    int px1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 165, r.getDisplayMetrics());
    button1.setWidth(px1);
    button2.setWidth(px1);
    button3.setWidth(px1);
    button4.setWidth(px1);

    Resources e = getResources();
    int px2 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 125, r.getDisplayMetrics());
    button1.setHeight(px2);
    button2.setHeight(px2);
    button3.setHeight(px2);
    button4.setHeight(px2);


    if (currentQuestion == 0){
        setQuestion(listOfQuestions.get(0));
    }

    button1.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View V){
                    gajiens(button1.getText().toString(), listOfQuestions.get(currentQuestion));
                    currentQuestion++;
                }
            }
    );

    button2.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View V){
                    gajiens(button2.getText().toString(), listOfQuestions.get(currentQuestion));
                    currentQuestion++;
                }
            }
    );

    button3.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View V){
                    gajiens(button3.getText().toString(), listOfQuestions.get(currentQuestion));
                    currentQuestion++;
                }
            }
    );

    button4.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View V){
                    gajiens(button4.getText().toString(), listOfQuestions.get(currentQuestion));
                    currentQuestion++;
                }
            }
    );


}

public void gajiens(String answer, Question thisQuestion){
    if (currentQuestion < 14){

        if (answer.equals(thisQuestion.getAnswer())){
            totalCorrect += "Question: " + thisQuestion.getQuestion() + "\nYour Answer: " + answer + "\n";
        } else {
            totalWrong += "Question: " + thisQuestion.getQuestion()) + "\nYour Answer: " + answer + "\n";
        }

        currentQuestion++;
        setQuestion(listOfQuestions.get(currentQuestion));
    } else {
        Intent intent = new Intent(this, EndActivity.class);
        intent.putExtra("correct", totalCorrect);
        intent.putExtra("wrong", totalWrong);
        startActivity(intent);
    }
}

public void setQuestion(Question kursh){
    QuestionText.setText(kursh.getJautajums());
    button1.setText(kursh.getOption1());
    button2.setText(kursh.getOption2());
    button3.setText(kursh.getOption3());
    button4.setText(kursh.getOption4());
}

对象问题是:

public Question(String Question, String Option1, String Option2, String Option3,String Option4, String correctAnswer){
    Question = Question;
    Option1 = Option1;
    Option2 = Option2;
    Option3 = Option3;
    Option4 = Option4;
    correctAnswer = correctAnswer;
}

基本上问题是App不算正确的答案。出于某种原因,它大部分时间使用TextView的原始文本作为“correctAnswer”。任何人都知道该怎么办?我怀疑,因为这不能正常工作,这不是特别好的方法,所以也许有人可以提出更好的方法吗?

1 个答案:

答案 0 :(得分:0)

TextView 的值与字符串进行比较,您可以按以下方式执行此操作

TextView tvAnswer = (TextView) findViewById(R.id.tvAnswer);
String correctAnswer = "Correct Answer";
if(correctAnswer.equals(tvAnswer.getText.toString()) )
 {
   //do something
 }
 else{
   //do something
  }