我创建了一个简单的测验应用程序,如下所示,它包含TextView
以显示问题,RadioGroup
包含3 RadioButtons
3个选项。完成测验后,它会显示正确答案的数量以及错误答案的数量和学位。但是我有以下问题请帮我解决。
问题:
MainActivity
public class MainActivity extends ActionBarActivity {
TextView tv;
Button btnNext;
RadioGroup rg;
RadioButton rb1,rb2,rb3;
String questions[]={"qqqqq","dddddd"};
String ans[]={"",""};
String opt[]={"","","","","",""};
int flag=0;
public static int marks,correct,wrong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tvque);
btnNext = (Button) findViewById(R.id.button1);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
rb1 = (RadioButton) findViewById(R.id.radio0);
rb2 = (RadioButton) findViewById(R.id.radio1);
rb3 = (RadioButton) findViewById(R.id.radio2);
tv.setText(questions[flag]);
rb1.setText(opt[0]);
rb2.setText(opt[1]);
rb3.setText(opt[2]);
Toast.makeText(this,"Nigative mark", 1000).show();
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
String ansText=uans.getText().toString();
if(ansText.equalsIgnoreCase(ans[flag]))
{
correct++;
}
else
{
wrong++;
}
flag++;
if(flag<questions.length)
{
tv.setText(questions[flag]);
rb1.setText(opt[flag*3]);
rb2.setText(opt[(flag*3)+1]);
rb3.setText(opt[(flag*3)+2]);
}
else
{
marks=correct;
}
Intent i=new Intent(getApplicationContext(),ResultActivity.class);
startActivity(i);
}
});
}
}
ResultActivity
public class ResultActivity extends ActionBarActivity {
TextView tv;
Button btRestart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tv=(TextView) findViewById(R.id.textView1);
btRestart=(Button) findViewById(R.id.button1);
StringBuffer sb=new StringBuffer();
sb.append("Correct ANS:"+MainActivity.correct);
sb.append("Wrong ANS:"+MainActivity.wrong);
sb.append("Final Score:"+MainActivity.marks);
tv.setText(sb);
MainActivity.correct=0;
MainActivity.wrong=0;
btRestart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
});
}
}
答案 0 :(得分:0)
当我点击下一个问题时,它会将我转移到结果活动而不是下一个问题。
您告诉它转到ResultsActivity
Intent
醇>
// 2nd param is the target Activity
Intent i=new Intent(getApplicationContext(),ResultActivity.class);
startActivity(i);
要解决此问题,您需要使用Activity
的名称来替换ResultActivity
。除非您将它们存储在Array
或其他类型的列表中,否则您只需将问题TextView
替换为下一个问题。
“结果”活动页面未正确显示总结果
如果不知道您的预期和实际结果,这几乎是不可能回答的。