14

ユーザーがボタンをクリックしたときにどのラジオボタンが選択されたかを検出する必要があるボタンの onClickListener があります。現在、以下の onClickListener に表示されている Log.v は、役に立たない情報を返していません。

これは、毎回異なるラジオを選択して送信を 3 回クリックしています。

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

        }
    }   
};
4

4 に答える 4

28

getCheckedRadioButtonId()でチェックされている (チェックされていない場合)のをid返します。レイアウトで個別の ID を設定すると、それらの ID とメソッドの戻り値を照合して、どれがチェックされているかを確認します。RadioButton-1RadioButtonsRadiogroupRadioButons

//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;
    }
于 2012-04-27T19:37:56.083 に答える
3

チェックされた ID を保存し、switch ステートメントまたは if-else チェーンを使用して関数 radioButton.getID() を使用して各ボタンと比較します

于 2012-04-27T19:38:25.943 に答える
1

RadioGroupxmlでチェック済みの最初の radioButton を設定しています

 <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
于 2019-03-06T18:49:57.793 に答える