getCheckedRadioButtonId()返回无用的诠释? [英] getCheckedRadioButtonId() returning useless int?
问题描述
我有一个需要检测哪个单选按钮是当用户点击该按钮选择按钮的onClickListener。目前,你看到下面的onClickListener的Log.v没有返回的信息一个无用位:
这是点击提交三次不同的无线电每个选定的时间:
04-27 19:24:42.417:V /提交(1564):1094168584
04-27 19:24:45.048:V /提交(1564):1094167752
04-27 19:24:47.348:V /提交(1564):1094211304
所以,我需要知道哪个单选按钮实际上是选择 - 有没有办法让单选按钮的对象呢?我希望能够得到它的编号从XML,以及它目前的案文。
下面是相关的code:
公共无效buildQuestions(JSONObject的问题)抛出JSONException {
RadioGroup中=(RadioGroup中)questionBox.findViewById(R.id.responseRadioGroup);
按钮chartsButton =(按钮)questionBox.findViewById(R.id.chartsButton);
chartsButton.setTag(问题);
按钮提交按钮=(按钮)questionBox.findViewById(R.id.submitButton);
chartsButton.setOnClickListener(chartsListener);
submitButton.setOnClickListener(submitListener);
TagObj tagObj =新TagObj(的问题,RadioGroup中);
submitButton.setTag(tagObj);
}
公共OnClickListener submitListener =新OnClickListener(){
公共无效的onClick(视图v){
userFunctions =新UserFunctions();
如果(userFunctions.isUserLoggedIn(活性)){
TagObj tagObject =(TagObj)v.getTag();
RadioGroup中RadioGroup中= tagObject.getRadioGroup();
JSONObject的问题= tagObject.getQuestion();
Log.v(提交,Integer.toString(radioGroup.getCheckedRadioButtonId()));
SubmitTask submitTask =新SubmitTask((轮询)活动的问题);
submitTask.execute();
}
}
};
getCheckedRadioButtonId()
返回 ID
的在单选
(或 1
如果没有单选按钮
检查),它在 RadioGroup中检查
。如果您在布局中设置不同的ID来的 RadioButons
,那么你将尝试匹配这些ID的方法的返回,看看哪一个被选中:
//领域中的类
私有静态最终诠释RB1_ID = 1000; //第一个单选按钮ID
私有静态最终诠释RB2_ID = 1001; //第二个单选按钮ID
私有静态最终诠释RB3_ID = 1002; //第三个单选按钮的ID
//创建单选按钮
单选按钮RB1 =新的单选按钮(这一点);
//设置ID
rb1.setId(RB1_ID);
INT BTN = radioGroup.getCheckedRadioButtonId();
开关(BTN){
案例RB1_ID:
//第一个单选按钮被选中。
打破;
//其他检查从RadioGroup中其他单选按钮IDS
情况1:
//没有单选按钮被选中在矿井RadioGroup中
打破;
}
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/submit(1564): 1094168584
04-27 19:24:45.048: V/submit(1564): 1094167752
04-27 19:24:47.348: V/submit(1564): 1094211304
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.
Here's the relevant code:
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()
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()返回无用的诠释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!