How to check if a radiobutton is checked in a radiogroup in Android?(如何检查单选按钮是否在 Android 的单选组中被选中?)
问题描述
我需要设置用户必须填写/选择页面中所有详细信息的验证.如果任何字段为空想显示 Toast 消息来填充.
现在我需要在 RadioGroup 中为
我尝试了此代码,但没有不能正常工作.建议我正确的方法.谢谢.RadioButton
设置验证.
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");
}
推荐答案
如果你只想检查一个 RadioButton
你可以使用 isChecked
函数
If you want to check on just one RadioButton
you can use the isChecked
function
if(radioButton.isChecked())
{
// is checked
}
else
{
// not checked
}
如果你有一个 RadioGroup
你可以使用
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 的单选组中被选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查单选按钮是否在 Android 的单选组中被选中?


- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01