getCheckedRadioButtonId() returning useless int?

2019-01-24 07:55发布

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();

        }
    }   
};

3条回答
forever°为你锁心
2楼-- · 2019-01-24 08:14

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;
    }
查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-24 08:21

I think relying on what radioGroup.getCheckedRadioButtonId() returns is not good practice if you want to store it into the database or to use it.

Because:

  • getCheckedRadioButtonId() value will keep changing for each RadioButton and if there are two similar values (two views in the same hierarchy) Android will choose the first one. Unless you provided a unique Ids with method generateViewId() and set it to the view with setId().
  • getCheckedRadioButtonId() will return unknown value.

Therefore

Switching on radioGroup.getCheckedRadioButtonId() and implement your custom values to each selection then use that custom value, not the View Id.

Example to use values from selected Radio Button:

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());

Example to populate selected RadioButton to UI:

    switch (selected) {
        case 0:
            oneRadioButton.setChecked(true);
            break;
        case 1:
            twoRadioButton.setChecked(true);
            break;
        case 2:
            threeRadioButton.setChecked(true);
            break;
    }
查看更多
乱世女痞
4楼-- · 2019-01-24 08:32

store the checked ID, then compare it to each button using the function radioButton.getID() using a switch statement or if-else chains

查看更多
登录 后发表回答