我有四个单选按钮,下一个按钮和一个问题。现在我的问题是,如果用户在没有选择任何选项的情况下单击next按钮,它应该显示一条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();
}
}发布于 2014-04-05 19:49:50
您应该只在一个条件内进行检查。您的RadioGroup中的任何RadioButton是否被选中。
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
https://stackoverflow.com/questions/22880369
复制相似问题