2014-01-29 30 views
0

我很困惑在哪里放这段代码,以便用户在选择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); 
} 
+0

将它放在方法中,并在onResume()中调用它,也在单选按钮的clicklistener中调用它。 – Mikel

回答

1
if(rda.isChecked()||rdb.isChecked()||rdc.isChecked()){ 

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(); 
       } 
} 

inOnCheckChanged

radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(RadioGroup arg0, int arg1) { 
      // TODO Auto-generated method stub 
      if(rda.isChecked()||rdb.isChecked()||rdc.isChecked()){ 
       butNext.setEnabled(true); 
      }else{ 
       butNext.setEnabled(false); 
      } 
     } 
    }); 
0

为您的曲目只需单击此单选按钮即可启动Intent。

对于防爆:使用条件检查,这将确保您的无线框已被检查..

if(radio_b.ischecked()){ 
// start Intent over here 
}else{ 
// Toast a message to user to select same 
} 

我希望这将清除您的疑问。

至于放置代码的问题,你可以把它放在onCreate();仅限方法。这实际上取决于你的需要。

干杯!

1

你可以简单的称之为你的RadioGroup的OnCheckedChangeListener()方法。

radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(RadioGroup arg0, int arg1) { 
     // TODO Auto-generated method stub 
     if(rda.isChecked()||rdb.isChecked()||rdc.isChecked()){ 
      butNext.setEnabled(true); 
     }else{ 
      butNext.setEnabled(false); 
     } 
    } 
}); 
+0

先生,我会准确地把这个代码? 对不起,即使在调用onClick事件的按钮之前oncrete()方法上的android – MasutaPogi

+0

。 – Piyush

+0

我得到了错误sir – MasutaPogi

相关问题