单选按钮只显示第一个选项正确,其他按钮显示android中的所有错误数据

时间:2012-01-11 06:36:03

标签: android radio-button

在我的问题回答小游戏我正在显示数据库文件中的问题和答案。 问题是在文本视图中,答案在radiobuttons.no数据显示我想要的方式,但当我选择数据时,只有当它是正确的答案时,单选按钮的第一个选项才选择正确的答案。我mainactivity中的代码如下`showQuestion =(TextView)findViewById(R.id.tvQuestions);         showAnswer =(TextView)findViewById(R.id.tvShowAnswer);

    radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
     r0 = (RadioButton) findViewById(R.id.radio0);
    r1 = (RadioButton) findViewById(R.id.radio1);
    r2 = (RadioButton) findViewById(R.id.radio2);
    r3 = (RadioButton) findViewById(R.id.radio3);
    bsubmit = (Button) findViewById(R.id.bsubmit);
    bsubmit.setOnClickListener(this);

              String shoow = myDb.makeatext(levels, Qno);
    String showextra1 = myDb.makeExtra1(levels, Qno);
    String showextra2 = myDb.makeExtra2(levels, Qno);
    String showextra3 = myDb.makeExtra3(levels, Qno);
    String showextra4 = myDb.makeExtra4(levels, Qno);

    // String showextra4 =myDb.makeanswers(levels, Qno);

    showQuestion.setText(shoow);
    r0.setText(showextra1);

    r1.setText(showextra2);

    r2.setText(showextra3);

    r3.setText(showextra4);

并且在onclick()方法中尝试检查数据但是如果数据只是正确的话,它只显示第一个单选按钮应答是正确的。

public void onClick(View v) {
    strat = System.currentTimeMillis();
    data = myDb.makeanswers(levels, Qno);
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bsubmit:
        if (r0.isChecked() == true)

        {
            if (r0.getText().toString().equalsIgnoreCase(data)) {
                showAnswer.setText("your answer is correct:" +r0.getText());

            } else {
                showAnswer.setText("your answer is wrong.the answer is:"
                        + data);

            }
        }
        if (r1.isChecked() == true)

        {
            if (r1.getText().toString().equalsIgnoreCase(data)) {
                showAnswer.setText("your answer is correct:" + r1.getText());

            } else {
                showAnswer.setText("your answer is wrong.the answer is:"
                        + data);

            }

        }
        if (r2.isChecked() == true)

        {
            if (r2.getText().toString().equalsIgnoreCase(data)) {
                showAnswer.setText("your answer is correct:" + r2.getText());

            } else {
                showAnswer.setText("your answer is wrong.the answer is:"
                        + data);

            }

        }
        if (r3.isChecked() == true)

        {
            if (r3.getText().toString().equalsIgnoreCase(data)) {
                showAnswer.setText("your answer is correct:" + r3.getText());

            } else {
                showAnswer.setText("your answer is wrong.the answer is:"
                        + data);

            }
        }}}

PLZ帮助我摆脱它我是android的新手。这将是一个很好的帮助

感谢, 麦迪。

3 个答案:

答案 0 :(得分:1)

请尝试这样做

case R.id.save_profile:
        if(((RadioButton)findViewById(R.id.radio0)).isChecked()){
            Answer=(RadioButton)findViewById(R.id.radio0)).getText().toString();
        }
       if(((RadioButton)findViewById(R.id.radio1)).isChecked()){
            Answer=(RadioButton)findViewById(R.id.radio0)).getText().toString();
       }  

        // compare here your option......


break;  

之后,您需要将答案与选中的按钮进行比较。

答案 1 :(得分:0)

你的代码很完美。这个对我有用。只需检查数据库检索和方法makeanswer()是否正确。

答案 2 :(得分:0)

   rb1=(RadioButton)findViewById(R.id.rb1);
   rb2=(RadioButton)findViewById(R.id.rb2);
   rb3=(RadioButton)findViewById(R.id.rb3);
   rb4=(RadioButton)findViewById(R.id.rb4);

rb1.setText("");//get from database options  
rb2.setText("");//get from database options  
rb3.setText("");//get from database options  
rb4.setText("");//get from database options  


//***********onclick of submit button{

int rd1=r_group.getCheckedRadioButtonId();
            String textt = "" ;
/************for error if not click on radio btn**********************/
            if(rd1==-1)

            {
                {   
                AlertDialog ad=new AlertDialog.Builder(Mind_Boost_First.this).create();
                ad.setTitle("error");
                ad.setMessage("plz select any radio button");
                ad.setIcon(R.drawable.cross);
                ad.setButton("ok",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                        // TODO Auto-generated method stub

                    }
                });
                ad.show();
                }}  
/****************************** get radio checked to textt**************************/
            else
            {

                switch(rd1)
                {
                    case R.id.rb1:
                textt =rb1.getText().toString().trim();
                        Log.e("rrrrrrrrbbbbbbbb11111111",""+rb1.getText().toString().trim());
                        break;
                    case R.id.rb2:
                textt = rb2.getText().toString().trim();
                        Log.e("rrrrrrrrbbbbbbbb222222222",""+rb1.getText().toString().trim());
                        break;
                    case R.id.rb3:
                textt = rb3.getText().toString();
                        Log.e("rrrrrrrrbbbbbbbb3333333333",""+rb1.getText().toString().trim());
                        break;
                    case R.id.rb4:
                textt = rb4.getText().toString();
                        Log.e("rrrrrrrrbbbbbbbb4444444444",""+rb1.getText().toString().trim());
                        break;
                  }


/*get correct value in str*/

                String str=c.getString(6).trim();

                r_group.clearCheck();




                /*reset value of lable */

这是我的代码片段,非常适合我