如果在android中单击,则更改单选按钮的文本颜色

时间:2014-03-16 11:27:36

标签: android

如果用户点击,我想更改单选按钮的文本颜色。但是我可以改变背景颜色,但我不想那样......任何人都可以告诉如何根据选择的正确或错误的选择放置十字或右标......? 这是我的主类代码..

final RadioButton Red = (RadioButton)findViewById(R.id.radioButton1);
    //RadioButton Blue = (RadioButton)findViewById(R.id.radioButton2);
    RadioButton Green = (RadioButton)findViewById(R.id.radioButton3);


    Red.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean      isChecked) {
        if(isChecked) {
            Red.setBackgroundColor(Color.RED);
        }

        }
    });
    Green.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

        }
    });

这是我的布局文件:

<RadioGroup
    android:id="@+id/radiogroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" 
        />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />
</RadioGroup>

2 个答案:

答案 0 :(得分:6)

更改radiobutton的文字颜色:

Red.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            Red.setTextColor(Color.RED);
        }
    }
});

对于自定义图标;在您的drawable文件夹中创建一个radiobutton.xml,其中包含:{/ p>

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

并将这些行放在styles.xml(在values文件夹中)中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomTheme" parent="android:Theme">
        <item name="android:radioButtonStyle">@style/RadioButton</item>
    </style>
    <style name="RadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
        <item name="android:button">@drawable/radiobutton</item>
    </style>
</resources>

答案 1 :(得分:0)

    RadioGroup question1RadioGroup = (RadioGroup) findViewById(R.id.radiogroup1);
    question1RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup view, @IdRes int checkedId) {
            RadioButton Red = (RadioButton) findViewById(R.id.radioButton1);
            RadioButton Green = (RadioButton) findViewById(R.id.radioButton3);

            if (checkedId == R.id.radioButton1) {
                Red.setTextColor(getResources().getColor(R.color.colorRed));
                Red.setBackgroundResource(R.drawable.wrong_option);
            } else if (checkedId == R.id.radioButton3) {
                Green.setTextColor(getResources().getColor(R.color.colorGreen));
                Green.setBackgroundResource(R.drawable.correct_option);
            }
        }
    });