如何检查是否一个单选按钮是在Android的一个RadioGroup中检查? [英] How to check if a radiobutton is checked in a radiogroup in Android?

查看:645
本文介绍了如何检查是否一个单选按钮是在Android的一个RadioGroup中检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置验证的用户必须填写/选择页面中的所有细节。如果有任何字段为空想告诉吐司消息来填补。在<$ C现在我需要设置验证单选 $ C> RadioGroup中。这个我试过code,但没有正常工作。我建议正确的方法。三江源。

  //获取选定的单选按钮,从RadioGroup中
INT selectedId = gender.getCheckedRadioButtonId();
//找到单选按钮由归国ID
selectedRadioButton =(单选)findViewById(selectedId);
//你想要做什么用radioButtonText(将它保存到数据库的情况下)
。radioButtonText = selectedRadioButton.getText()的toString();

如果(radioButtonText.matches())
{
    Toast.makeText(getApplicationContext(),请选择性别,Toast.LENGTH_SHORT).show();
    Log.d(QAOD,性别为空);
}
其他
{
    Log.d(QAOD,性别选择);
}
 

解决方案

如果您想检查只是一个单选您可以使用器isChecked 功能

 如果(radioButton.isChecked())
{
  \\检查
}
其他
{
  \\未选中
}
 

如果你有一个 RadioGroup中您可以使用

 如果(radioGroup.getCheckedRadioButtonId()== -1)
{
  \\没有单选按钮被选中
}
其他
{
  \\的一个单选按钮被选中
}
 

I need to set validation that user must fill / select all details in a page. If any fields are empty wanna show Toast message to fill. Now I need set validation for RadioButton in the RadioGroup. I tried this code but didn't work properly. Suggest me correct way. Thankyou.

// get selected radio button from radioGroup
int selectedId = gender.getCheckedRadioButtonId();
// find the radiobutton by returned id
selectedRadioButton = (RadioButton)findViewById(selectedId);
// do what you want with radioButtonText (save it to database in your case)
radioButtonText = selectedRadioButton.getText().toString();

if(radioButtonText.matches(""))
{
    Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
    Log.d("QAOD", "Gender is Null");
}
else
{
    Log.d("QAOD", "Gender is Selected");
}

解决方案

If you want to check on just one RadioButton you can use the isChecked function

if(radioButton.isChecked())
{
  \\ is checked    
}
else
{
  \\ not checked
}

and if you have a RadioGroup you can use

if (radioGroup.getCheckedRadioButtonId() == -1)
{
  \\ no radio buttons are checked
}
else
{
  \\ one of the radio buttons is checked
}

这篇关于如何检查是否一个单选按钮是在Android的一个RadioGroup中检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆