【问题标题】:Changing Text color of RadioButton if clicked in android如果在 android 中单击,则更改 RadioButton 的文本颜色
【发布时间】:2014-03-16 11:27:35
【问题描述】:

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

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>

【问题讨论】:

    标签: android


    【解决方案1】:

    更改单选按钮的文本颜色:

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

    用于自定义图标;在您的可绘制文件夹中创建一个radiobutton.xml,其中包含以下内容:

    <?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>
    

    【讨论】:

      【解决方案2】:
          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);
                  }
              }
          });
      

      【讨论】:

        猜你喜欢
        • 2014-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多