[英]How to make an Editable into a Boolean
I'm attempting to make an interactive quiz and I have an EditText view that is an Editable in my Java code. 我正在尝试进行交互式测验,并且我有一个EditText视图,该视图在我的Java代码中是可编辑的 。 So long as the user inputs an answer in this field (min. 10 characters), he/she is going to get it correct.
只要用户在该字段中输入答案(最少10个字符),他/她就可以正确回答。 However, my code is telling me that I need to set up the Editable as a Boolean in my calculateScore method.
但是,我的代码告诉我,我需要在calculateScore方法中将Editable设置为布尔值 。
Anyone know how to make the Editable as a Boolean in my calculateScore method while also checking if the minimum character limit is met? 有谁知道如何在我的calculateScore方法中使Editable作为布尔值 ,同时还要检查是否满足最小字符数限制?
package com.example.android.quantummechanicsquiz;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void radioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radio_one_correct:
if (checked) break;
case R.id.radio_one_b:
if (checked) break;
case R.id.radio_one_c:
if (checked) break;
case R.id.radio_one_d:
if (checked) break;
case R.id.radio_two_a:
if (checked) break;
case R.id.radio_two_b:
if (checked) break;
case R.id.radio_two_correct:
if (checked) break;
case R.id.radio_two_d:
if (checked) break;
case R.id.radio_four_a:
if (checked) break;
case R.id.radio_four_b:
if (checked) break;
case R.id.radio_four_correct:
if (checked) break;
case R.id.radio_four_d:
if (checked) break;
case R.id.radio_five_a:
if (checked) break;
case R.id.radio_five_b:
if (checked) break;
case R.id.radio_five_c:
if (checked) break;
case R.id.radio_five_correct:
if (checked) break;
}
}
private int calculateScore(boolean answerOne, boolean answerTwo, boolean answerThreeA,
boolean answerThreeB, boolean answerThreeC, boolean answerThreeD,
boolean answerFour, boolean answerFive, Editable answerSix) {
score = 100;
if (answerOne) {
} else {
score = score - 100 / 6;
}
if (answerTwo) {
} else {
score = score - 100 / 6;
}
if (answerThreeA) {
score = score - 8;
} else {
}
if (answerThreeB) {
} else {
score = score + 4;
}
if (answerThreeC) {
} else {
score = score + 4;
}
if (answerThreeD) {
score = score - 8;
} else {
}
if (answerFour) {
} else {
score = score - 100 / 6;
}
if (answerFive) {
} else {
score = score - 100 / 6;
}
if (answerSix) {
} else {
score = score - 100 / 6;
}
return score;
}
public void submitScore(View view) {
RadioButton answerOne = (RadioButton) findViewById(R.id.radio_one_correct);
boolean correctAnswerOne = answerOne.isChecked();
RadioButton answerTwo = (RadioButton) findViewById(R.id.radio_two_correct);
boolean correctAnswerTwo = answerTwo.isChecked();
CheckBox answerThreeA = (CheckBox) findViewById(R.id.checkbox_three_a);
boolean incorrectAnswerThreeA = answerThreeA.isChecked();
CheckBox answerThreeB = (CheckBox) findViewById(R.id.checkbox_three_b_correct);
boolean correctAnswerThreeB = answerThreeB.isChecked();
CheckBox answerThreeC = (CheckBox) findViewById(R.id.checkbox_three_c_correct);
boolean correctAnswerThreeC = answerThreeC.isChecked();
CheckBox answerThreeD = (CheckBox) findViewById(R.id.checkbox_three_d);
boolean incorrectAnswerThreeD = answerThreeD.isChecked();
RadioButton answerFour = (RadioButton) findViewById(R.id.radio_four_correct);
boolean correctAnswerFour = answerFour.isChecked();
RadioButton answerFive = (RadioButton) findViewById(R.id.radio_five_correct);
boolean correctAnswerFive = answerFive.isChecked();
EditText answerSix = (EditText) findViewById(R.id.question_six_edit_text);
Editable correctAnswerSix = answerSix.getEditableText();
int finalScore = calculateScore(correctAnswerOne, correctAnswerTwo,
incorrectAnswerThreeA, correctAnswerThreeB, correctAnswerThreeC,
incorrectAnswerThreeD, correctAnswerFour, correctAnswerFive, correctAnswerSix);
Toast.makeText(this, "Congratulations! You have a score of " + finalScore + " out of " +
"100", Toast.LENGTH_LONG).show();
}
}
Thanks. 谢谢。
if (answerSix) {
// something
} else {
score = score - 100 / 6;
}
answerSix is an Editable
- you're thinking of, say, JS where something undefined === false answerSix是
Editable
-您想到的是JS,其中某些未定义=== false
You could do something like if (!answerSix.toString().equals("")) ...
(if I'm understanding what you're looking for correctly) 您可以做类似
if (!answerSix.toString().equals("")) ...
(如果我正确理解了您要寻找的内容)
尝试这个:
boolean correctAnswerSix = answerSix.length > 9;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.