我很困惑在哪裏放這段代碼,以便用戶在選擇radiobutton
之前不會去其他頁面。不知道把這段代碼放在我的java文件中的位置
這是代碼,我不知道該放:
if(rda.isChecked() == true)
{
butNext.setEnabled(true);
}
if(rdb.isChecked() == true)
{
butNext.setEnabled(true);
}
if(rdc.isChecked() == true)
{
butNext.setEnabled(true);
}
else
{
butNext.setEnabled(false);
}
這是我的全部代碼或MainAct.java
:
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
RadioGroup radioGroup1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
DBase db=new DBase(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.rda);
rdb=(RadioButton)findViewById(R.id.rdb);
rdc=(RadioButton)findViewById(R.id.rdc);
butNext=(Button)findViewById(R.id.button1);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{
score++;
Log.d("score", "Your score"+score);
}
if(qid<10)
{
currentQ=quesList.get(qid);
setQuestionView();
}
else
{
Intent intent = new Intent(MainAct.this, activity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
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.main, menu);
return true;
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
qid++;
rda.setChecked(false);
rdb.setChecked(false);
rdc.setChecked(false);
//butNext.setEnabled(false);
}
將它放在方法中,並在onResume()中調用它,也在單選按鈕的clicklistener中調用它。 – Mikel