【问题标题】:Adding custom view with radio button to radio group将带有单选按钮的自定义视图添加到单选组
【发布时间】:2012-08-04 06:57:43
【问题描述】:

我有一个自定义视图,其中包含一个 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 答案并帮助他人:

对于我添加到 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 View 设置为 clickable:false。

【问题讨论】:

    标签: android radio-button radio-group


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      当您向 RadioGroup 添加视图时,只有当视图是 RadioButton 的实例时,该组才能正常工作。在您的情况下,您正在添加一个 LinearLayout。所以 SingleRadioItem 应该扩展 RadioButton。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-10
        • 2015-03-15
        • 2018-09-14
        • 2014-01-09
        • 1970-01-01
        • 2012-05-20
        • 2016-07-10
        • 1970-01-01
        相关资源
        最近更新 更多