单击下一个按钮时如何检查单选按钮值

时间:2014-04-05 11:35:16

标签: android button onclicklistener oncheckedchanged

我的情况是我有四个单选按钮,下一个按钮和一个问题。现在我的问题是,如果用户点击下一个按钮而没有选择任何选项,它应该显示一个toast消息,如果用户点击任何一个单选按钮,应该检查它是对还是错。我已部分实施,但我无法得到确切的解决方案。任何人都可以帮助我。

提前致谢

我已在下面发布了我的代码

Next.setOnClickListener(new View.OnClickListener() 
     {
         @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub



                count++;

                if(viewFlipper.getDisplayedChild()==0)
                {
                    id1 = rag1.getCheckedRadioButtonId();
                    rab1=(RadioButton)findViewById(id1);
                    //System.out.println("1nd question answer"+rab1.getText());

                    if (rag1.getCheckedRadioButtonId()!=R.id.btn1_1||rag1.getCheckedRadioButtonId()!=R.id.btn1_2||rag1.getCheckedRadioButtonId()!=R.id.btn1_3||rag1.getCheckedRadioButtonId()!=R.id.btn1_4||rag1.getCheckedRadioButtonId()!=R.id.btn1_5&&rag1.getCheckedRadioButtonId()==-1)
                    {
                        Toast.makeText(getApplicationContext(), "Please enter the  answer", Toast.LENGTH_SHORT).show();
                    }



                     if((rag1.getCheckedRadioButtonId()!=-1)&&rab1.getText().equals(c_alcorrectanswer.get(0)))
                     {
                         System.out.println("1nd question answer"+rab1.getText());
                     }

                    else
                    {
                        Toast.makeText(getApplicationContext(), "Please enter the correct answer", Toast.LENGTH_SHORT).show();
                        viewFlipper.stopFlipping();
                    }



                }

1 个答案:

答案 0 :(得分:1)

您应该只在一个条件下检查。是否选择了RadioButton RadioGroup {/ 1}}。

  int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
  if(radioButtonID > 0) //return -1 if none of radiobutton is selected from radiogroup
   {
     //move to next
   }
  else
  {
   // show toast. Please select any option.
  }

根据您的代码段:

if(viewFlipper.getDisplayedChild()==0)
{
 id1 = rag1.getCheckedRadioButtonId();
    if(id1 < 0) //return -1 if none of radio button is selected
     {
     //Toast. Please select any answer
     }
 }

developer.android.com documentation reference