【问题标题】:Android RadioGroup behaves differently when RadioButtons added programmatically以编程方式添加 RadioButtons 时,Android RadioGroup 的行为有所不同
【发布时间】:2017-08-14 22:22:21
【问题描述】:

我正在创建一个自定义 RadioGroup,定义如下。

MyRadioGroup.cpp

public class MyRadioGroup
        extends RadioGroup
{
...
    public MyRadioGroup(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mContext = context;
        init(attrs);
    }

    private void init(AttributeSet attrs)
    {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.my_radio_group, this);
    }

    public String getSelection()
    {
        View radioButton = findViewById(getCheckedRadioButtonId());
        return radioButton.getTag().toString();
    }
}

my_radio_group.xml 有几个按钮定义如下...

<merge
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RadioButton
        android:id="@+id/rbOne"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@drawable/rb_selection_background"
        android:button="@null"
        android:gravity="center_horizontal"
        android:tag="One"
        android:text="Radio Button 1"
        android:textColor="@color/rptText"
        android:textSize="18sp"/>
...
</merge>

rb_selection_background.xml 可绘制

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/simple_border"
        android:state_checked="true"/>
</selector>

通过此设置,我得到了我想要的没有圆形按钮的结果,并且所选按钮在文本周围显示了一个简单的边框。

然后我进行了更改以从字符串数组资源动态生成我的单选按钮。像这样……

private void init(AttributeSet attrs)
{

    Drawable selectionBg = ResourcesCompat.getDrawable(getResources(),
                                              R.drawable.rb_selection_background,
                                                   null);

    String[] rbStrings = getResources().getStringArray(R.array.rb_strings);
    for(int i = 0; i < rbStrings.length; ++i)
    {
        RadioButton rb = new RadioButton(mContext);
        LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
        lp.weight = 1;
        rb.setText(rbStrings[i]);
        rb.setTag(rbStrings[i]);
        rb.setTextSize(18);
        rb.setGravity(Gravity.CENTER_HORIZONTAL);
        rb.setButtonDrawable(0);
        rb.setBackground(selectionBg);
        addView(rb, lp);
    }
}

现在无论我点击哪个单选按钮,简单边框总是显示在最后一个单选按钮上。单选组的状态是正确的,因为当我调用 getSelection() 时,它确实返回了被选中单选按钮的标签。

回到我的问题,为什么行为不同?以编程方式添加单选按钮时,有没有办法获得我想要的行为?谢谢。

【问题讨论】:

    标签: android radio-group


    【解决方案1】:

    变化

    rb.setBackground(selectionBg);
    

    rb.setBackgroundResource(R.drawable.rb_selection_background);
    

    工作。

    我没有解释为什么,我需要进一步研究每种方法。

    【讨论】:

      猜你喜欢
      • 2012-09-11
      • 2012-06-12
      • 2012-02-15
      • 1970-01-01
      • 2011-10-02
      • 2018-04-17
      • 1970-01-01
      • 2016-05-24
      • 2013-11-24
      相关资源
      最近更新 更多