我一直在做一个Android测试,所有这些都是我主要工作的。当用户选择他们的答案时,他们会被重定向到结果屏幕,告诉他们有多少是对是错。我想在我的练习模式中添加一个新功能,它会向他们显示他们选择的答案以及正确的答案。但我只是好奇如何调用变量
TextView tv;
Button btnNext;
RadioGroup rg;
RadioButton rb1, rb2, rb3;
String question[] = {"This classes course number", "What is the name of the program we used to make apps", "Android is a Which kind of software?"};
String ans[] = {" 2250", "Android Studio", "Operating System"};
String opt[] = {"2230", "2240", "2250", "Dev Kit 2010", "Android Creation", "Android Studio", "Operating System", "Antivirus", "Application"};
int flag = 0;
public static int marks, correct, wrong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_csits);
tv = (TextView) findViewById(R.id.tvque);
btnNext = (Button) findViewById(R.id.buttonNext);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb2 = (RadioButton) findViewById(R.id.radioButton2);
rb3 = (RadioButton) findViewById(R.id.radioButton3);
tv.setText(question[flag]);
rb1.setText(opt[0]);
rb2.setText(opt[1]);
rb3.setText(opt[2]);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton your_answers = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String ansText = your_answers.getText().toString();
marks = correct - wrong;
if (ansText.equalsIgnoreCase(ans[flag])) {
correct++;
} else {
wrong++;
}
flag++;
if (flag < question.length) {
tv.setText(question[flag]);
rb1.setText(opt[flag * 3]);
rb2.setText(opt[(flag * 3) + 1]);
rb3.setText(opt[(flag * 3) + 2]);
} else {
marks = correct - wrong;
Intent intent = new Intent(CSITSActivity.this, ResultsActivity.class);
startActivity(intent);
}
}
});
}
}
答案 0 :(得分:0)
不确定这是否是您尝试做的,因为您的问题不是那么清楚,但如果您尝试将值从一个意图传递到另一个意图,您可以将所选答案放在一个数组中并将它们传递给新意图像这样:
String[] chosenAnswers = {"a","a","b"}
Intent startIntent = new Intent(this,ResultsActivity.class);
startIntent.putExtra("chosen", chosenAnswers); //"chosen" is a random tag that can be anything but is used to get back the value of the list in the new intent
this.startActivity(startIntent);
然后,从ResultsActivity.class中,您可以使用以下方法获取该数组的值:
Intent i = getIntent();
String[] chosenAnswers = i.getStringArrayExtra("chosen");
这也可以使用相同的方法使用ArrayList来完成,除非在新的intent使用中获取列表:
i.getStringArrayListExtr(chosen);
不确定这是否是你想要实现的目标,但希望它有所帮助。