我正在为10个问题创建一个Android测验应用程序,但每次我点击屏幕上的下一个按钮以显示新问题时,新问题的单选按钮会以某种方式保存上一个问题的信息。即使我检查新问题的右侧单选按钮,它显示不正确,但如果我选择第三个单选按钮,则表示答案是正确的。我尝试了不同的东西,但结果仍然相同。请帮忙。
public class MainActivity2 extends ActionBarActivity {
private Questions[] questions = new Questions[10];
private int qNum = 0;
Button next;
RadioButton op1, op2, op3, op4;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
imageView = (ImageView)findViewById(R.id.imageView2);
imageView.setImageResource(R.drawable.saturn);
initQuestions();
initNextButton();
initQuestionText();
answers();
}
private void initNextButton(){
final RadioButton rb1 = (RadioButton)findViewById(R.id.op1RBtn);
final RadioButton rb2 = (RadioButton)findViewById(R.id.op2RBtn);
final RadioButton rb3 = (RadioButton)findViewById(R.id.op3RBtn);
final RadioButton rb4 = (RadioButton)findViewById(R.id.op4RBtn);
final TextView textView2 = (TextView)findViewById(R.id.textView2);
final RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup);
Button button = (Button)findViewById(R.id.nextBtn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
qNum++;
initQuestionText();
initOptions();
if(questions[qNum] == questions[1]){
textView2.setText("");
rg.clearCheck();
rb1.setChecked(false);
rb1.invalidate();
rb2.setChecked(false);
rb2.invalidate();
rb3.setChecked(false);
rb3.invalidate();
rb4.setChecked(false);
rb4.invalidate();
}
if(questions[qNum] == questions[1]){
textView2.setText("");
rg.clearCheck();
rb1.setChecked(false);
rb1.invalidate();
rb2.setChecked(false);
rb2.invalidate();
rb3.setChecked(false);
rb3.invalidate();
rb4.setChecked(false);
rb4.invalidate();
}
if(questions[qNum] == questions[2]){
textView2.setText("");
rg.clearCheck();
rb1.setChecked(false);
rb1.invalidate();
rb2.setChecked(false);
rb2.invalidate();
rb3.setChecked(false);
rb3.invalidate();
rb4.setChecked(false);
rb4.invalidate();
}
if(questions[qNum] == questions[3]){
textView2.setText("");
rg.clearCheck();
rb1.setChecked(false);
rb1.invalidate();
rb2.setChecked(false);
rb2.invalidate();
rb3.setChecked(false);
rb3.invalidate();
rb4.setChecked(false);
rb4.invalidate();
}
if(questions[qNum] == questions[4]){
textView2.setText("");
rg.clearCheck();
rb1.setChecked(false);
rb1.invalidate();
rb2.setChecked(false);
rb2.invalidate();
rb3.setChecked(false);
rb3.invalidate();
rb4.setChecked(false);
rb4.invalidate();
}
if(questions[qNum] == questions[5]){
textView2.setText("");
rg.clearCheck();
rb1.setChecked(false);
rb1.invalidate();
rb2.setChecked(false);
rb2.invalidate();
rb3.setChecked(false);
rb3.invalidate();
rb4.setChecked(false);
rb4.invalidate();
}
if(questions[qNum] == questions[6]){
textView2.setText("");
rg.clearCheck();
rb1.setChecked(false);
rb1.invalidate();
rb2.setChecked(false);
rb2.invalidate();
rb3.setChecked(false);
rb3.invalidate();
rb4.setChecked(false);
rb4.invalidate();
}
if(questions[qNum] == questions[7]){
textView2.setText("");
rg.clearCheck();
rb1.setChecked(false);
rb1.invalidate();
rb2.setChecked(false);
rb2.invalidate();
rb3.setChecked(false);
rb3.invalidate();
rb4.setChecked(false);
rb4.invalidate();
}
if(questions[qNum] == questions[8]){
textView2.setText("");
rg.clearCheck();
rb1.setChecked(false);
rb1.invalidate();
rb2.setChecked(false);
rb2.invalidate();
rb3.setChecked(false);
rb3.invalidate();
rb4.setChecked(false);
rb4.invalidate();
}
if(questions[qNum] == questions[9]){
textView2.setText("");
rg.clearCheck();
rb1.setChecked(false);
rb1.invalidate();
rb2.setChecked(false);
rb2.invalidate();
rb3.setChecked(false);
rb3.invalidate();
rb4.setChecked(false);
rb4.invalidate();
Button fin = (Button)findViewById(R.id.nextBtn);
fin.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(MainActivity2.this, MainActivity3.class);
startActivity(intent);
}
});
}
}
});
}
private void initQuestionText(){
TextView textView = (TextView)findViewById(R.id.textView3);
textView.setText(questions[qNum].getQuery());
}
private void initQuestions(){
questions[0] = new Questions("What is the fifth planet in the solar system?");
questions[1] = new Questions("What is the chemical symbol for Sodium?");
questions[2] = new Questions("What is HCl?");
questions[3] = new Questions("How many bones in a human body?");
questions[4] = new Questions("How many teeth does an average human have?");
questions[5] = new Questions("What came first? Chicken or the Egg?");
questions[6] = new Questions("What is high in citric acid?");
questions[7] = new Questions("What family does the whale belong to?");
questions[8] = new Questions("What is water made of?");
questions[9] = new Questions("What percentage of Nitrogen does the atmosphere contain?");
}
private void answers(){
final RadioButton rb1 = (RadioButton)findViewById(R.id.op1RBtn);
final RadioButton rb2 = (RadioButton)findViewById(R.id.op2RBtn);
final RadioButton rb3 = (RadioButton)findViewById(R.id.op3RBtn);
final RadioButton rb4 = (RadioButton)findViewById(R.id.op4RBtn);
RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup);
final TextView textView2 = (TextView)findViewById(R.id.textView2);
if(questions[qNum] == questions[0]){
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("YAY! YOU GOT IT!");
}
});
rb4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
}
if(questions[qNum] == questions[1]){
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("YAY! YOU GOT IT!");
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
}
});
}
if(questions[qNum] == questions[2]){
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("YAY! YOU GOT IT!");
}
});
rb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
}
if(questions[qNum] == questions[3]){
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("YAY! YOU GOT IT!");
}
});
}
if(questions[qNum] == questions[4]){
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("YAY! YOU GOT IT!");
}
});
rb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
}
if(questions[qNum] == questions[5]){
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("YAY! YOU GOT IT!");
}
});
rb4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
}
if(questions[qNum] == questions[6]){
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("YAY! YOU GOT IT!");
}
});
}
if(questions[qNum] == questions[7]){
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("YAY! YOU GOT IT!");
}
});
rb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
}
if(questions[qNum] == questions[8]){
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("YAY! YOU GOT IT!");
}
});
rb4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
}
if(questions[qNum] == questions[9]){
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("YAY! YOU GOT IT!");
}
});
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
rb4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("NOPE! SORRY! :(");
}
});
}
}
private void initOptions(){
imageView = (ImageView)findViewById(R.id.imageView2);
imageView.setImageResource(R.drawable.saturn);
if(questions[qNum] == questions[0]) {
op1 = (RadioButton)findViewById(R.id.op1RBtn);
op1.setText("Neptune");
op2 = (RadioButton)findViewById(R.id.op2RBtn);
op2.setText("Saturn");
op3 = (RadioButton)findViewById(R.id.op3RBtn);
op3.setText("Jupiter");
op4 = (RadioButton)findViewById(R.id.op4RBtn);
op4.setText("Earth");
}
else if(questions[qNum] == questions[1]) {
op1 = (RadioButton)findViewById(R.id.op1RBtn);
op1.setText("Na");
op2 = (RadioButton)findViewById(R.id.op2RBtn);
op2.setText("So");
op3 = (RadioButton)findViewById(R.id.op3RBtn);
op3.setText("Di");
op4 = (RadioButton)findViewById(R.id.op4RBtn);
op4.setText("Sa");
imageView.setImageResource(android.R.color.transparent);
imageView.setImageResource(R.drawable.sodium);
}
else if(questions[qNum] == questions[2]) {
op1 = (RadioButton)findViewById(R.id.op1RBtn);
op1.setText("Base");
op2 = (RadioButton)findViewById(R.id.op2RBtn);
op2.setText("Acid");
op3 = (RadioButton)findViewById(R.id.op3RBtn);
op3.setText("Solid");
op4 = (RadioButton)findViewById(R.id.op4RBtn);
op4.setText("Liquid");
imageView.setImageResource(android.R.color.transparent);
imageView.setImageResource(R.drawable.acid);
}
else if(questions[qNum] == questions[3]) {
op1 = (RadioButton)findViewById(R.id.op1RBtn);
op1.setText("200");
op2 = (RadioButton)findViewById(R.id.op2RBtn);
op2.setText("183");
op3 = (RadioButton)findViewById(R.id.op3RBtn);
op3.setText("300");
op4 = (RadioButton)findViewById(R.id.op4RBtn);
op4.setText("206");
imageView.setImageResource(android.R.color.transparent);
imageView.setImageResource(R.drawable.skeleton);
}
else if(questions[qNum] == questions[4]) {
op1 = (RadioButton)findViewById(R.id.op1RBtn);
op1.setText("20");
op2 = (RadioButton)findViewById(R.id.op2RBtn);
op2.setText("32");
op3 = (RadioButton)findViewById(R.id.op3RBtn);
op3.setText("34");
op4 = (RadioButton)findViewById(R.id.op4RBtn);
op4.setText("38");
imageView.setImageResource(android.R.color.transparent);
imageView.setImageResource(R.drawable.tooth);
}
else if(questions[qNum] == questions[5]) {
op1 = (RadioButton)findViewById(R.id.op1RBtn);
op1.setText("Chicken");
op2 = (RadioButton)findViewById(R.id.op2RBtn);
op2.setText("Egg");
op3 = (RadioButton)findViewById(R.id.op3RBtn);
op3.setText("I Don't Know");
op4 = (RadioButton)findViewById(R.id.op4RBtn);
op4.setText("The Hen Came First");
imageView.setImageResource(android.R.color.transparent);
imageView.setImageResource(R.drawable.chicken);
}
else if(questions[qNum] == questions[6]) {
op1 = (RadioButton)findViewById(R.id.op1RBtn);
op1.setText("Apple");
op2 = (RadioButton)findViewById(R.id.op2RBtn);
op2.setText("Mango");
op3 = (RadioButton)findViewById(R.id.op3RBtn);
op3.setText("Banana");
op4 = (RadioButton)findViewById(R.id.op4RBtn);
op4.setText("Oranges");
imageView.setImageResource(android.R.color.transparent);
imageView.setImageResource(R.drawable.fruits);
}
else if(questions[qNum] == questions[7]) {
op1 = (RadioButton)findViewById(R.id.op1RBtn);
op1.setText("Reptile");
op2 = (RadioButton)findViewById(R.id.op2RBtn);
op2.setText("Mammal");
op3 = (RadioButton)findViewById(R.id.op3RBtn);
op3.setText("Amphibians");
op4 = (RadioButton)findViewById(R.id.op4RBtn);
op4.setText("Cave Fish");
imageView.setImageResource(android.R.color.transparent);
imageView.setImageResource(R.drawable.whale);
}
else if(questions[qNum] == questions[8]) {
op1 = (RadioButton)findViewById(R.id.op1RBtn);
op1.setText("CO2 and O");
op2 = (RadioButton)findViewById(R.id.op2RBtn);
op2.setText("H and O");
op3 = (RadioButton)findViewById(R.id.op3RBtn);
op3.setText("H2 and O");
op4 = (RadioButton)findViewById(R.id.op4RBtn);
op4.setText("CH4 and H");
imageView.setImageResource(android.R.color.transparent);
imageView.setImageResource(R.drawable.water);
}
else {
op1 = (RadioButton)findViewById(R.id.op1RBtn);
op1.setText("78%");
op2 = (RadioButton)findViewById(R.id.op2RBtn);
op2.setText("55%");
op3 = (RadioButton)findViewById(R.id.op3RBtn);
op3.setText("34%");
op4 = (RadioButton)findViewById(R.id.op4RBtn);
op4.setText("88%");
imageView.setImageResource(android.R.color.transparent);
imageView.setImageResource(R.drawable.air);
next = (Button)findViewById(R.id.nextBtn);
next.setText("");
next.setText("Finish");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
尝试这种方式,无需使用点击单选按钮
RadioGroup group=(RadioGroup) findViewById(R.id.radioGroup1);
group.setOnCheckedChangeListener(
new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(radiobutton1.isChecked()) {
// put your code here
} else if(radiobutton2.isChecked()) {
// put your code here
}
}
});