簡體   English   中英

比較TextView String時equals方法不起作用

[英]equals method doesn't work when comparing TextView String

所以我是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”。 有人知道該怎么辦嗎? 我懷疑由於這無法正常工作,因此這並不是最佳方法,因此也許有人可以建議一個更好的方法?

可以將TextView的值與String進行比較,如下所示

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM