如何重置单选按钮的背景色?

时间:2019-03-30 07:45:52

标签: android radio-button background-color

我只是想重置单选按钮的背景色。我已经为单选按钮及其文本和背景设置了完美的颜色。后来要重置单选按钮的文本颜色,我使用了 ColorStateList ,它仅对文本颜色有效,而对背景无效重置。 谁能建议我如何重置单选按钮的背景色?还有其他类似** COlorStateList **的方法来重置背景吗?

谢谢。

我还在下面附加了我的代码:

package com.hfad.rdiobuttontest;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
RadioButton radioButton1,radioButton2,radioButton3;
RadioGroup radioGroup;
TextView question;
Button button, button2;

private ColorStateList defaulttextcolor;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    radioButton1=findViewById(R.id.radio_button1);
    radioButton2=findViewById(R.id.radio_button2);
    radioButton3=findViewById(R.id.radio_button3);
    radioGroup=findViewById(R.id.radio_group);
    question=findViewById(R.id.question);
    button=findViewById(R.id.test);
    button2=findViewById(R.id.reset);

    defaulttextcolor=radioButton1.getTextColors();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            radiobuttoncolor ();

        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            radioButton1.setTextColor(defaulttextcolor);
            radioButton2.setTextColor(defaulttextcolor);
            radioButton3.setTextColor(defaulttextcolor);


            radioGroup.clearCheck();

            question.setText("result- background still same");
            radioButton1.setText("my backgroud color did not           changed,/n can you please help me to cahnge it?");
            radioButton2.setText("textcolr has been reset");
            radioButton3.setText("opps, 1 - ur backgroud is still blue");

        }
      });

   }

   public void radiobuttoncolor (){
   radioButton1.setTextColor(defaulttextcolor);
   radioButton2.setTextColor(defaulttextcolor);
   radioButton3.setTextColor(defaulttextcolor);


   radioGroup.clearCheck();

   question.setText("please click on color reset button");
   radioButton1.setText("My backgroud color is blue");
   radioButton1.setTextColor(Color.GREEN);
   radioButton1.setBackgroundColor(Color.BLUE);
   radioButton2.setText("I am green");
   radioButton2.setTextColor(Color.GREEN);
   radioButton3.setText("1) backgroud blue, \n2) text green \n3)i am default ");

   }

   }

而不是布局

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:padding="16dp"
tools:context="com.hfad.rdiobuttontest.MainActivity">


<TextView
    android:id="@+id/question"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/radio_group"
    android:layout_marginBottom="16dp"
    android:freezesText="true"
    android:text="please click on color test button"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="20sp" />

<RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    >

    <RadioButton
        android:id="@+id/radio_button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:freezesText="true"
        android:text="Option 1"
        android:padding="6dp"
        android:layout_marginTop="10dp"
        />

    <RadioButton
        android:id="@+id/radio_button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:checked="false"
        android:freezesText="true"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layoutDirection="ltr"
        android:padding="6dp"
        android:text="Option 2"
        android:textColor="@color/colorAccent"

        />

    <RadioButton
        android:id="@+id/radio_button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:freezesText="true"
        android:text="Option 3"
        android:padding="6dp"/>


</RadioGroup>

<Button
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/radio_group"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="91dp"
    android:freezesText="true"
    android:text="colortest" />

<Button
    android:id="@+id/reset"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/radio_group"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="140dp"
    android:freezesText="true"
    android:text="Color reset" />

1 个答案:

答案 0 :(得分:2)

有两种方法可以实现这一目标,

要重置颜色,可以使用透明颜色,也可以使用单选按钮的默认颜色。因此,在您的点击监听器中尝试一下-

radioButton1.setBackgroundColor(android.R.drawable.btn_radio); 

radioButton1.setBackgroundColor(0x00000000);