因此,我正在尝试创建一个多项选择测验,它将根据您的选择生成一个健身计划。我有三个多项选择按钮,每次回答一个问题时,问题和答案都会轮换。我正在尝试获取用户为每个问题选择的答案,但是我的程序不允许我正确地获取数据。
我尝试过使用getter和setter方法,但我的程序仍然无法获取信息
private TextView blank;
private Button mButtonChoice1;
private Button mButtonChoice2;
private Button mButtonChoice3;
String experience;
String preference;
int days;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
blank = (TextView) findViewById(R.id.program);
mButtonChoice1 = (Button)findViewById(R.id.choice1);
mButtonChoice2 = (Button)findViewById(R.id.choice2);
mButtonChoice3 = (Button)findViewById(R.id.choice3);
mButtonChoice1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice1.getText() == "Two"){
//QUESTION 1 ANSWER_1
updateQuestion();
days = 2;
}
else if (mButtonChoice1.getText() == "0-6 months") { //QUESTION 2 ANSWER_1
updateQuestion();
experience = "0-6 months";
}
}
mButtonChoice2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice2.getText() == "Three"){ //QUESTION 1 ANSWER_2
updateQuestion();
days = 3;
}
else if(mButtonChoice2.getText() == "6-18 months"){ //QUESTION 2 ANSWER_2
updateQuestion();
experience = "6-18 months";
}
}
}
mButtonChoice3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//My logic for Button goes in here
if (mButtonChoice3.getText() == "Four"){ //QUESTION 1 ANSWER_3
updateQuestion();
days = 4;
}
else if (mButtonChoice3.getText() == "1.5+ years") { //QUESTION 2 ANSWER_3
updateQuestion();
experience = "1.5+ years";
}
}
}
//here i am just testing if my program is able to receive the data that was entered for each variable
if (day == 2 && preference == "Strength" && experience == "0-6 months") {
blank.setText("test working");
}
else {
blank.setText("Test not working");
}
}// oncreate
然而,就在我启动程序的时候,甚至在我选择任何选项之前,测试testVIew部分就已经声明了“测试不工作”。
如果我将if语句放在onclickListener函数中,我的程序将能够获得变量的值,但是,我需要将if语句放在底部,因为我需要考虑每个变量的所有情况,并且不能将if语句放在onclick方法中。
我希望能够在onClickListener之外获得变量preference,days,experience
的值
发布于 2019-04-03 08:25:32
尝试:
if (day == 2 || preference == "Strength" || experience == "0-6 months")
或
if(day ==2 ){
statements;
}
else if(preference == "Strength"){
statements;
}
else if(experience == "0-6 months"){
statements;
}
else {
blank.setText("Test not working");
}
发布于 2019-04-03 08:46:49
在你的onCreate中,你只是在按钮中设置了监听器,当你检查答案时,屏幕上仍然没有显示活动。该活动在您可以看到按钮等之前进行检查,因为检查是在onCreate中进行的。
你应该在按下按钮后进行检查。例如,在您的updateQuestion()
中,您检测到最后一个问题已经得到回答,然后进行检查并在此时显示结果。
https://stackoverflow.com/questions/55490177
复制