4

他のビューの中でも RadioButton を保持するカスタム ビューがあります。

SingleRadioItem:

public class SingleRadioItem extends LinearLayout {
    private TextView mTextKey;
    private RadioButton mRadioButton;
    private ImageView mImageSeparator;

    public SingleRadioItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.rtl_single_radio_item, this, true);

        mTextKey = (TextView)view.findViewById(R.id.single_radio_item_text_key);
        mRadioButton = (RadioButton)view.findViewById(R.id.single_radio_item_button);
        mImageSeparator = (ImageView)view.findViewById(R.id.single_image_separator);
    }

    public void setKey(String key) {
        mTextKey.setText(key);
    }

    public boolean getSelectedState() {
        return mRadioButton.isSelected();
    }

    public void setSelectedState(boolean selected) {
        mRadioButton.setSelected(selected);
    }
}

このビューのインスタンスを作成し、それらを RadioGroup に追加し、RadioGroup を LinearLayout に追加したいと考えています。これを行うと、すべてのラジオ ボタンを選択した状態に設定できます。つまり、RadioGroup がうまく機能していません (おそらく、私のやり方が原因です..)

RadioGroup radioGroup = new RadioGroup(this);
        radioGroup.setOrientation(RadioGroup.VERTICAL);

        SingleRadioItem radio1 = new SingleRadioItem(this, null);
        SingleRadioItem radio2 = new SingleRadioItem(this, null);

        radioGroup.addView(radio1);
        radioGroup.addView(radio2);

        updateDetailsView.addView(radioGroup);

明らかにRadioButton radio1 = new RadioButton(this);、RadioGroup を追加するとうまく機能します。

ラジオボタンを保持するビューをラジオグループに追加することさえ可能ですか?何かが欠けているか、まったく不可能ですか?

ありがとう!

解決策: @cosmincalistru Answer を拡張して他のユーザーを支援するには:

LinearLayout に追加した SingleRadioItem ごとに、次のようなリスナーをアタッチしました。

radio1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (lastRadioChecked != null) {
                    lastRadioChecked.setCheckedState(false);
                }

                lastRadioChecked = (SingleRadioItem)v;
                lastRadioChecked.setCheckedState(true);
            }
        });

また、SingleRadioItem XML 内の RadioButton ビューを clickable:false に設定する必要があります。

4

2 に答える 2

3

RadioButton は、RadioGroup に直接従属する必要があります。そうしないと、ボタンは別のグループのものと見なされます。最良のアイデアは、各 RadioButton でリスナーを使用することです。

編集: RadioButtons のグループをグループの一部として作成したいが、RadioGroup を使用できないときはいつでも、次のようにします:

RadioButton r1,r2,....;
// Instantiate all your buttons;
...
// Set listener on each
for(each RadioButton) {
    rx.setOnCheckedChangeListener(OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                //set all buttons to false;
                for(each RadioButton) {
                    rx.setChecked(false);
                }
                //set new selected button to true;
                buttonView.setChecked(true);
            }
        }
    });
}
于 2012-08-07T09:32:55.620 に答える
0

ビューを RadioGroup に追加すると、ビューが RadioButton のインスタンスである場合にのみ、グループが正しく機能します。あなたの場合、LinearLayout を追加しています。したがって、SingleRadioItem は RadioButton を拡張する必要があります。

于 2012-08-07T09:32:52.660 に答える