简体   繁体   English

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

[英]How to change background color/shape of a Radiobutton when selected/deselected

I am adding radiobuttons programatically to a RadioGroup in a for loop. 我在for循环中以编程方式将radiobuttons添加到RadioGroup。 I want my buttons to have a certain background color when not checked, and another when they are checked (only one can be selected at once). 我希望我的按钮在未选中时具有特定的背景颜色,而在检查时则需要另一个背景颜色(一次只能选择一个)。 My problem is that I also want this background to be a rounded rectangle. 我的问题是我也希望这个背景是一个圆角矩形。 I have it working partially, but whenever I select an answer, it will set that button's background to white, but it won't reset any of the other buttons. 我让它部分工作,但每当我选择一个答案时,它会将该按钮的背景设置为白色,但它不会重置任何其他按钮。

I understand that I need some type of selector object, to add the toggle behavior that is expected from the radiogroup. 我知道我需要某种类型的选择器对象,以添加从radiogroup预期的切换行为。 I was able to find some examples of that, but they only showed how to set a solid color as a background. 我能够找到一些例子,但他们只展示了如何将纯色设置为背景。 I need to use this rounded corner shape. 我需要使用这个圆角形状。 So, my trouble is figuring out how to blend the two. 所以,我的麻烦在于弄清楚如何融合这两者。 I could be going about this completely wrong. 我可能会完全错误。 If there is a better approach to achieving what I want, I would prefer to use that rather than salvage what I've already tried. 如果有更好的方法来实现我想要的东西,我宁愿使用它而不是打捞我已经尝试过的东西。

I was able to get close by creating two XML files, each with a shape object and its own background color. 我能够通过创建两个XML文件来接近,每个文件都有一个形状对象和它自己的背景颜色。 If a radiobutton is selected, its style gets set to the radio_button_checked" style. But, I know I am approaching this wrong because the other checked button does not toggle off. I can basically click all of the buttons and get them all to have white backgrounds. I know there is a better way to do this, I just don't know what it is 如果选择了radiobutton,它的样式将被设置为radio_button_checked“样式。但是,我知道我接近这个错误,因为另一个选中的按钮没有关闭。我基本上可以点击所有按钮并让它们全部变白背景。我知道有更好的方法可以做到这一点,我只是不知道它是什么

Here is the loop where I add the buttons, along with the change listener for radiogroup rg (rg is my RadioGroup and answers is just a HashMap of strings. They don't affect anything related to this problem. The buttonTintList line is just there to change circle color) 这是我添加按钮的循环,以及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));

        }
    });

Here is the XML for unchecked buttons (radiobutton_style_unchecked.xml) 这是用于未选中按钮的XML(radiobutton_style_unchecked.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
<corners android:radius="6dp" />
<gradient
    android:angle="45"
    android:centerColor="@color/colorPrimaryLight"
    android:centerX="35%"
    android:endColor="@color/colorPrimaryLight"
    android:startColor="@color/colorPrimaryLight"
    android:type="linear" />

<padding
    android:bottom="5dp"
    android:left="0dp"
    android:right="0dp"
    android:top="5dp" />


</shape>

The style for checked buttons is exactly the same, but with white instead of colorPrimaryLight 选中按钮的样式完全相同,但使用white而不是colorPrimaryLight

Here is what it looks like with nothing selected: 这是什么看起来没有选择: 1

Here is what it looks like when one item is selected. 这是选择一个项目时的样子。 2

And this is what happens when I select more than one 当我选择多个时会发生这种情况 3

Try this 试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_light"
    android:orientation="vertical">

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/radio_background"
            android:padding="10dp"
            android:text="Yes" />

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/radio_background"
            android:padding="10dp"
            android:text="No" />

    </RadioGroup>

</LinearLayout>

@drawable/radio_background @绘制/ radio_background

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" android:state_checked="true" />
    <item android:drawable="@color/colorAccent" android:state_checked="false" />
</selector>

OUTPUT OUTPUT

在此输入图像描述

UPDATE UPDATE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_light"
    android:orientation="vertical">

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/test"
            android:padding="10dp"
            android:text="Yes" />

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/test"
            android:padding="10dp"
            android:text="No" />

    </RadioGroup>

</LinearLayout>

@drawable/test @绘制/测试

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

    <item android:drawable="@drawable/radio_selected" android:state_checked="true" />
    <item android:drawable="@drawable/radio_normal" />

</selector>

@drawable/radio_selected @绘制/ radio_selected

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dp" />
    <solid android:color="@android:color/white" />
    <padding
        android:bottom="5dp"
        android:left="0dp"
        android:right="0dp"
        android:top="5dp" />


</shape>

@drawable/radio_normal @绘制/ radio_normal

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dp" />
    <solid android:color="@color/colorAccent" />
    <padding
        android:bottom="5dp"
        android:left="0dp"
        android:right="0dp"
        android:top="5dp" />

</shape>

OUTPUT OUTPUT

在此输入图像描述

I suggest you have 3 drawable xml 我建议你有3个可绘制的xml

  1. radio_checked.xml(the effect you want to be checked) radio_checked.xml(要检查的效果)
  2. radio_unchecked.xml (the effect you want to be unchecked) radio_unchecked.xml(您想要取消选中的效果)
  3. radio_custom_drawable.xml radio_custom_drawable.xml

    In this xml you have to do something like this: 在这个xml中你必须做这样的事情:

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

And then in your code change this 然后在你的代码中改变这个

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM