2014-01-29 80 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

相關問題