选择/取消选择时如何更改单选按钮的背景颜色/形状

我正在以编程方式将单选按钮添加到 for 循环中的 RadioGroup 中。我希望我的按钮在未选中时具有某种背景颜色,而在选中时具有另一种背景颜色(一次只能选择一个)。我的问题是我也希望这个背景是一个圆角矩形。我让它部分工作,但每当我选择一个答案时,它会将该按钮的背景设置为白色,但不会重置任何其他按钮。


我知道我需要某种类型的选择器对象来添加无线电组所期望的切换行为。我能够找到一些例子,但他们只展示了如何将纯色设置为背景。我需要使用这种圆角形状。所以,我的麻烦是弄清楚如何将两者融合在一起。我可能完全错了。如果有更好的方法来实现我想要的,我宁愿使用它而不是挽救我已经尝试过的东西。


我可以通过创建两个 XML 文件来接近,每个文件都有一个形状对象和自己的背景颜色。如果选择了一个单选按钮,它的样式将设置为 radio_button_checked" 样式。但是,我知道我正在接近这个错误,因为另一个选中的按钮没有关闭。我基本上可以单击所有按钮并让它们都变成白色背景。我知道有更好的方法可以做到这一点,我只是不知道它是什么


这是我添加按钮的循环,以及 radiogroup rg 的更改侦听器(rg 是我的 RadioGroup,答案只是字符串的 HashMap。它们不会影响与此问题相关的任何内容。buttonTintList 行就在那里改变圆圈颜色)


for(int i = 0; i < answers.size(); i++)

    {

        RadioButton rb = new RadioButton(context);

        rb.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            rb.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor( context, R.color.colorAccent)));

        }

        rb.setBackground(context.getResources().getDrawable(R.drawable.radiobutton_style_unchecked));

        rb.setText(answers.get(i));

        rb.setWidth(800);

        rb.setHeight(150);

        rb.setTextSize(18);

        rb.setLayoutParams(params);

        rg.addView(rb);

    }

    /*  Store the answer */

    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override

        public void onCheckedChanged(RadioGroup group, int checkedId) {

            RadioButton rb= (RadioButton)group.findViewById(checkedId);

            answer = rb.getText().toString();


            rb.setBackground(context.getResources().getDrawable(R.drawable.radiobutton_style_checked));


        }

    });



富国沪深
浏览 128回答 2
2回答

慕哥6287543

试试这个<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:background="@android:color/holo_blue_light"&nbsp; &nbsp; android:orientation="vertical">&nbsp; &nbsp; <RadioGroup&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:orientation="vertical">&nbsp; &nbsp; &nbsp; &nbsp; <RadioButton&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_margin="10dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/radio_background"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:padding="10dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="Yes" />&nbsp; &nbsp; &nbsp; &nbsp; <RadioButton&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_margin="10dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/radio_background"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:padding="10dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="No" />&nbsp; &nbsp; </RadioGroup></LinearLayout>@drawable/radio_background<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">&nbsp; &nbsp; <item android:drawable="@android:color/white" android:state_checked="true" />&nbsp; &nbsp; <item android:drawable="@color/colorAccent" android:state_checked="false" /></selector>输出更新<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:background="@android:color/holo_blue_light"&nbsp; &nbsp; android:orientation="vertical">&nbsp; &nbsp; <RadioGroup&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:orientation="vertical">&nbsp; &nbsp; &nbsp; &nbsp; <RadioButton&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_margin="10dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/test"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:padding="10dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="Yes" />&nbsp; &nbsp; &nbsp; &nbsp; <RadioButton&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_margin="10dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/test"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:padding="10dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="No" />&nbsp; &nbsp; </RadioGroup></LinearLayout>@drawable/测试<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">&nbsp; &nbsp; <item android:drawable="@drawable/radio_selected" android:state_checked="true" />&nbsp; &nbsp; <item android:drawable="@drawable/radio_normal" /></selector>@drawable/radio_selected<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:shape="rectangle">&nbsp; &nbsp; <corners android:radius="20dp" />&nbsp; &nbsp; <solid android:color="@android:color/white" />&nbsp; &nbsp; <padding&nbsp; &nbsp; &nbsp; &nbsp; android:bottom="5dp"&nbsp; &nbsp; &nbsp; &nbsp; android:left="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:right="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:top="5dp" /></shape>@drawable/radio_normal<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:shape="rectangle">&nbsp; &nbsp; <corners android:radius="20dp" />&nbsp; &nbsp; <solid android:color="@color/colorAccent" />&nbsp; &nbsp; <padding&nbsp; &nbsp; &nbsp; &nbsp; android:bottom="5dp"&nbsp; &nbsp; &nbsp; &nbsp; android:left="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:right="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:top="5dp" /></shape>输出

拉莫斯之舞

我建议你有 3 个可绘制的 xmlradio_checked.xml(你要检查的效果)radio_unchecked.xml(你想取消选中的效果)radio_custom_drawable.xml在此 xml 中,您必须执行以下操作:<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="true"&nbsp;&nbsp;android:drawable="@drawable/radio_checked" /><item android:state_checked="false"&nbsp;&nbsp;android:drawable="@drawable/radio_unchecked" /></selector>然后在你的代码中改变这个rb.setBackground(context.getResources().getDrawable(R.drawable.radiobutton_style_checked));到rb.setBackground(context.getResources().getDrawable(R.drawable.radio_custom_drawable));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java