简体   繁体   中英

EditText Java Code in Android Studio

I'm building a quiz app in Android Studio. One of my questions has an EditText to write in the answer. I am stuck on how to determine the score for this answer. Is it a boolean or a string? Here is what I have:

    EditText q4 = (EditText)findViewById(R.id.answer4_Wakanda);
    Boolean q4RightAnswer = q4.isChecked();

I know this is wrong. I had a string and changed it to a boolean, which is where I got stuck. It's possible that the whole thing is wrong. I just want my app to recognize the right answer, in order to give it 1 point.

Any help is greatly appreciated. I am a new coder so I could really use a hand!

You can't call isChecked() on an EditText. To recognise the right answer you would simply have to compare the user's input in the EditText with the correct answer. Ie

String userAns = q4.getText().toString();
if (userAns.equals(correctAns)) {
    // correct, add 1 point
}
else {
    // incorrect, do something else
}

I hope this is what you are looking for, if not I would need some further clarification on what the issue is, or perhaps a larger portion of your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM