【发布时间】:2014-09-19 12:06:21
【问题描述】:
我需要设置用户必须填写/选择页面中所有详细信息的验证。如果任何字段为空想显示Toast message to fill. 现在我需要在RadioGroup. 中为RadioButton 设置验证我尝试了这段代码但没有正常工作。建议我正确的方法。谢谢。
// 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(radioGroup.getCheckedRadioButtonId()==radioGroup.getChildAt(radio1).getId()) { 收音机 1 被选中; } else if(..... == radioGroup.getChildAt(radio2)) { 收音机 2 被选中; } ...使用它,您可以获得选定的单选值。 :)
-
嗨@RipalTamboli 感谢您的评论,但我可以获得单选按钮的值和文本。我需要验证用户是否不检查性别(无线电组),单击按钮时会显示错误消息。
-
好的。你能检查一下,如果没有选择任何选项,那么 getCheckedRadioButtonID() 返回什么?如果它返回 null / -1,那么您的工作是通过检查它是否为 null / -1 来完成的。 :) 性别组中的 nwys,默认情况下始终需要选择一个选项。这是设计它的标准方式:) .. 如果有任何疑问,请告诉我。
标签: android radio-button radio-group