Вопрос по android-layout, android – getCheckedRadioButtonId () возвращает бесполезный int?

11

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

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

        }
    }   
};

Ваш Ответ

4   ответа
2

0

RadioGroup

 <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>

rgCustomized.checkedRadioButtonId==rbCustomized.id
20

getCheckedRadioButtonId()Error: User Rate Limit ExceededidError: User Rate Limit ExceededRadioButtonError: User Rate Limit Exceeded-1Error: User Rate Limit ExceededRadioButtonsError: User Rate Limit ExceededRadiogroupError: User Rate Limit ExceededRadioButonsError: User Rate Limit Exceeded

//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..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;
    }
Error: User Rate Limit Exceeded Davek804
Error: User Rate Limit Exceeded Davek804
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
0

radioGroup.getCheckedRadioButtonId()

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

radioGroup.getCheckedRadioButtonId()

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;
    }

Похожие вопросы