[英]Checking at least one radio button is selected from each radiogroup in android?
How do I ensure that user is checked at least one radiobutton from each radiogroup. 如何确保用户从每个放射组中至少检查一个radiobutton。 Like I have this radiogroup:
就像我有这个无线电组:
<RadioGroup
android:id="@+id/myradio_group_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="third" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fourt" />
</RadioGroup>
<RadioGroup
android:id="@+id/myradio_group_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="third" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fourt" />
</RadioGroup>
I want to check programmatically if user select at least one radiobutton or not? 我想以编程方式检查用户是否选择了至少一个radiobutton?
Get a reference of the RadioGroup
and then call getCheckedRadioButtonId()
on the radio group if nothing is selected then the call will return -1
. 获取
RadioGroup
的引用,然后在无线电组上调用getCheckedRadioButtonId()
如果未选择任何内容,则调用将返回-1
。
See public int getCheckedRadioButtonId ()
请参阅
public int getCheckedRadioButtonId ()
RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits);
// Returns an integer which represents the selected radio button's ID
int selected = g.getCheckedRadioButtonId();
// Gets a reference to our "selected" radio button
RadioButton b = (RadioButton) findViewById(selected);
// Now you can get the text or whatever you want from the "selected" radio button
b.getText();
if (radioGroup.getCheckedRadioButtonId() != -1)
{
// hurray at-least on radio button is checked.
}
else
{
// pls select at-least one radio button.. since id is -1 means no button is check
}
This is the code i have used in one of my quiz app.. Have a look at the code if this helps.. 这是我在我的一个测验应用程序中使用的代码..看看代码,如果这有帮助..
private boolean checkAnswer(){
String answer = getSelection();
if(answer==null) {
Log.d("Questions", "No checkbox selected");
return false;
}
else {
// Do your stuff here
return true;
}
}
private String getSelection(){
if (c1.isChecked())
{
return c1.getText().toString();
}
if (c2.isChecked())
{
return c2.getText().toString();
}
if (c3.isChecked())
{
return c3.getText().toString();
}
if (c4.isChecked())
{
return c4.getText().toString();
}
return null;
}
You can use this method 您可以使用此方法
radioGroup.getCheckedRadioButtonId();
It returns the identifier of the selected radio button in this group.
它返回该组中所选单选按钮的标识符。 Upon empty selection, the returned value is -1.
选择空时,返回值为-1。
As mentioned in the documentation https://developer.android.com/reference/android/widget/RadioGroup.html#getCheckedRadioButtonId() 如文档中所述https://developer.android.com/reference/android/widget/RadioGroup.html#getCheckedRadioButtonId()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.