getCheckedRadioButtonId() 返回无用的 int? [英] getCheckedRadioButtonId() returning useless int?
问题描述
我有一个按钮的 onClickListener 需要检测用户单击按钮时选择了哪个单选按钮.目前,您在 onClickListener 中看到的 Log.v 没有返回无用的信息:
I have a button's onClickListener that needs to detect which radiobutton was selected when the user clicks the button. Currently, the Log.v you see below in the onClickListener is not returning a useless bit of info:
这是点击提交三次,每次选择不同的电台:
This is clicking submit three times with a different radio selected each time:
04-27 19:24:42.417: V/提交(1564): 1094168584
04-27 19:24:42.417: V/submit(1564): 1094168584
04-27 19:24:45.048: V/提交(1564): 1094167752
04-27 19:24:45.048: V/submit(1564): 1094167752
04-27 19:24:47.348: V/提交(1564): 1094211304
04-27 19:24:47.348: V/submit(1564): 1094211304
所以,我需要知道实际选择了哪个单选按钮 - 有没有办法获取单选按钮的对象?我希望能够从 XML 中获取它的 id#,以及它的当前文本.
So, I need to know which radioButton is actually selected - is there a way to get the object of the radiobutton? I want to be able to get it's id# from XML, as well as its current text.
以下是相关代码:
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();
}
}
};
推荐答案
getCheckedRadioButtonId()
返回RadioButton
的id
(或-1
如果没有检查 RadioButtons
)在 Radiogroup
中检查.如果您在布局中为 RadioButons
设置不同的 id,那么您将尝试将这些 id 与方法的返回相匹配,以查看哪个被选中:
getCheckedRadioButtonId()
returns the id
of the RadioButton
(or -1
if no RadioButtons
are checked) that is checked in the Radiogroup
. If you set distinct ids to the RadioButons
in the layout then you will try to match those ids with the return of the method to see which one is checked:
//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;
}
这篇关于getCheckedRadioButtonId() 返回无用的 int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!