我有一个按钮的onClickListener,需要检测用户点击按钮时选择了哪个单选按钮。目前,您在onClickListener中看到的Log.v没有返回无用的信息:
这是点击提交三次,每次选择不同的电台:
04-27 19:24:42.417:V / submit(1564):1094168584
04-27 19:24:45.048:V / submit(1564):1094167752
04-27 19:24:47.348:V / submit(1564):1094211304
所以,我需要知道哪个radioButton实际被选中 - 有没有办法获得无线电按钮的对象?我希望能够从XML获取它的id#以及它的当前文本。
以下是相关代码:
public void buildQuestions(JSONObject question) throws JSONException {
radioGroup = (RadioGroup) questionBox.findViewById(R.id.responseRadioGroup);
Button chartsButton = (Button) questionBox.findViewById(R.id.chartsButton);
chartsButton.setTag(question);
Button submitButton = (Button) questionBox.findViewById(R.id.submitButton);
chartsButton.setOnClickListener(chartsListener);
submitButton.setOnClickListener(submitListener);
TagObj tagObj = new TagObj(question, radioGroup);
submitButton.setTag(tagObj);
}
public OnClickListener submitListener = new OnClickListener() {
public void onClick(View v) {
userFunctions = new UserFunctions();
if (userFunctions.isUserLoggedIn(activity)) {
TagObj tagObject = (TagObj) v.getTag();
RadioGroup radioGroup = tagObject.getRadioGroup();
JSONObject question = tagObject.getQuestion();
Log.v("submit", Integer.toString(radioGroup.getCheckedRadioButtonId()));
SubmitTask submitTask = new SubmitTask((Polling) activity, question);
submitTask.execute();
}
}
};
答案 0 :(得分:21)
getCheckedRadioButtonId()
会返回id
中RadioButton
的{{1}}(如果未选中-1
,则会RadioButtons
} Radiogroup
。如果您在布局中为RadioButons
设置了不同的ID,那么您将尝试将这些ID与方法的返回相匹配,以查看检查了哪一个:
//field in the class
private static final int RB1_ID = 1000;//first radio button id
private static final int RB2_ID = 1001;//second radio button id
private static final int RB3_ID = 1002;//third radio button id
//create the RadioButton
RadioButton rb1 = new RadioButton(this);
//set an id
rb1.setId(RB1_ID);
int btn = radioGroup.getCheckedRadioButtonId();
switch (btn) {
case RB1_ID:
// the first RadioButton is checked.
break;
//other checks for the other RadioButtons ids from the RadioGroup
case -1:
// no RadioButton is checked inthe Radiogroup
break;
}
答案 1 :(得分:2)
存储已检查的ID,然后使用函数radioButton.getID()使用switch语句或if-else链将其与每个按钮进行比较
答案 2 :(得分:0)
如果您要将radioGroup.getCheckedRadioButtonId()
存储在数据库中或使用它,我认为依靠getCheckedRadioButtonId()
的返回值不是一个好习惯。
原因:
RadioButton
的值将针对每个generateViewId()
不断变化,如果有两个相似的值(相同层次结构中的两个视图),Android将选择第一个。除非您使用方法setId()
提供了唯一的ID,然后使用getCheckedRadioButtonId()
将其设置为视图。radioGroup.getCheckedRadioButtonId()
将返回未知值。因此
打开int selected = -1;
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.one_radioButton:
selected = 0;
break;
case R.id.two_radioButton:
selected = 1;
break;
case R.id.three_radioButton:
selected = 2;
break;
}
// return a custom value you specific
Log.d(TAG, "selectedBox: " + selectedBox);
// return a random unknown number value
Log.d(TAG, "radioGroup.getCheckedRadioButtonId(): " + radioGroup.getCheckedRadioButtonId());
并为每个选择实现您的自定义值,然后使用该自定义值,而不是视图ID。
switch (selected) {
case 0:
oneRadioButton.setChecked(true);
break;
case 1:
twoRadioButton.setChecked(true);
break;
case 2:
threeRadioButton.setChecked(true);
break;
}
{{1}}
答案 3 :(得分:0)
我正在RadioGroup
xml中设置初始检查的单选按钮
<RadioGroup
android:id="@+id/rgCustomized"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:checkedButton="@+id/rbNotCustomized"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbCustomized"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:text="@string/yes" />
<RadioButton
android:id="@+id/rbNotCustomized"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no" />
</RadioGroup>
然后我确定选择了哪个radioButton
rgCustomized.checkedRadioButtonId==rbCustomized.id