不知道把这个代码放在我的java文件中的哪个位置

时间:2014-01-29 12:35:15

标签: java android eclipse

我很困惑放置此代码的位置,以便用户在选择radiobutton之前不会转到其他页面。

这是我不知道的代码:

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

3 个答案:

答案 0 :(得分: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);
            }
        }
    });

答案 1 :(得分: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);
        }
    }
});

答案 2 :(得分:0)

对于您的查询,您只需在单击此单选按钮时启动Intent。

对于Ex:使用条件检查,以确保您的收音机盒已被检查..

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

我希望这可以解除你的怀疑。

就放置代码而言,你可以将它放在onCreate();仅限方法。这实际上取决于你的需要。

干杯!