5

RadioGroup可変数の radioButtons を作成しました。問題は、多くのラジオボタンをチェックできることです.(私が知っているように)単一のRadioButtonチェックインではありませんRadioGroup.私の間違いはどこですか? これが私のコードです:

View view = inflater.inflate(R.layout.questionnaire_check_question, null);
RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
    for (int k = 0; k < answerValues.size(); k++) {
         View radioButtonView = inflater.inflate(R.layout.check_radio_button, null);
         RadioButton radioButton = (RadioButton) radioButtonView
                            .findViewById(R.id.radio_button);
         radioButton.setText(answerValues.get(k));
         radioGroup.addView(radioButtonView);
    }
questionBody.addView(view);

アンケート_チェック_質問.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:background="#A4A7A6" >

    <RadioGroup
        android:id="@+id/radioGroup"
        android:scrollbars="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </RadioGroup>

</LinearLayout>

check_radio_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radio_button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_marginLeft="5dp"
    android:button="@drawable/checkbox"
 >

</RadioButton>

drawable/checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/icon_checkbox_on" android:state_checked="true"/>
    <item android:drawable="@drawable/icon_checkbox_off" android:state_checked="false"/>

</selector>

radioGroup には奇妙な機能があります..最初の RadioGroup をチェックしてから 2 番目の RadioGroup をオンにすると、最初のチェックが外れてしまいます.この後、最初の RadioGroup を再度選択したい場合、これを行うことはできません..もうチェックしません。誰でも私を助けることができますか?どんなアイデアでも大歓迎です!前もって感謝します。

4

2 に答える 2

11

私は解決策を見つけました: 問題は、すべてのボタンが同じ ID を持っていることでした。そこで、radioButton ごとに一意の ID を設定しました。

于 2013-07-17T08:43:43.463 に答える
-2

これがどのように行われるかの例を示しています。次に、それに応じてコードを変更します。xml で、ラジオ ボタンを追加するには、以下のようなレイアウトを定義する必要があります -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" 
            android:checked="true" />

        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />

    </RadioGroup>

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />

</LinearLayout>

Java ファイルで、次のようにします。 i) 以下のように、ラジオ グループとラジオボタンを宣言します。

 private RadioGroup radioSexGroup;
  private RadioButton radioSexButton;
  private Button btn;

ii) 以下のような xml レイアウトでラジオボタンとラジオグループを登録します -

radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);

iii) 以下のようなボタンに onclicklistner を追加します -

btn.setOnClickListner(this);

iv) OnClickListner インターフェイスを実装し、OnClickListner インターフェイスの未実装のメソッドをオーバーライドする

v) オーバーライドされたメソッド OnClick で、次のようにコーディングします -

int selectedId = radioSexGroup.getCheckedRadioButtonId();

            // find the radiobutton by returned id
                radioSexButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(MyAndroidAppActivity.this,
                radioSexButton.getText(), Toast.LENGTH_SHORT).show();

そして、これはそれがどのように行われるかです..これが役立つことを願っています.

于 2013-07-17T08:08:25.827 に答える