0

我的情况是有四个单选按钮、下一个按钮和一个问题。现在我的问题是,如果用户在没有选择任何选项的情况下单击下一步按钮,它应该显示一条 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();
                    }



                }
4

1 回答 1

1

您应该只在一个条件内进行检查。无论您RadioButton的任何一个是否RadioGroup被选中。

  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 文档参考

于 2014-04-05T11:49:49.640 回答