更改单选按钮的文本颜色点击在android系统 [英] Changing Text color of RadioButton if clicked in android
本文介绍了更改单选按钮的文本颜色点击在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想如果用户单击更改单选按钮的文本颜色。不过,我可以改变背景颜色,但我不希望that..and谁能甚至会告诉如何把交叉或右侧标志按权利或选择错误的选择......?
这里是我的code主类。
最后单选红色=(单选)findViewById(R.id.radioButton1);
//单选按钮蓝色=(单选)findViewById(R.id.radioButton2);
单选按钮绿色=(单选)findViewById(R.id.radioButton3);
Red.setOnCheckedChangeListener(新OnCheckedChangeListener(){ @覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
如果(器isChecked){
Red.setBackgroundColor(Color.RED);
} }
});
Green.setOnCheckedChangeListener(新OnCheckedChangeListener(){ @覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
// TODO自动生成方法存根 }
});
这里是我的布局文件:
< RadioGroup中
机器人:ID =@ + ID / radiogroup1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT> <单选
机器人:ID =@ + ID / radioButton1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=单选按钮
/> <单选
机器人:ID =@ + ID / radioButton2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=单选/> <单选
机器人:ID =@ + ID / radioButton3
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=单选/>
< / RadioGroup中>
解决方案
要改变单选按钮的文本颜色:
Red.setOnCheckedChangeListener(新OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
如果(器isChecked){
Red.setTextColor(Color.RED);
}
}
});
有关自定义图标;创建一个 radiobutton.xml
在这个它绘制文件夹:
<?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:可绘制=@绘制/ checked_icon机器人:state_checked =真/>
<项目机器人:可绘制=@绘制/ unchecked_icon机器人:state_checked =FALSE/>
< /选择>
和把这些线在你的 styles.xml
(在值
文件夹):
<?XML版本=1.0编码=UTF-8&GT?;
<资源>
<样式名称=CustomTheme父=安卓主题>
<项目名称=机器人:radioButtonStyle> @风格/单选< /项目>
< /风格>
<样式名称=单选按钮父=@安卓风格/ Widget.CompoundButton.RadioButton>
<项目名称=机器人:按钮> @绘制/单选< /项目>
< /风格>
< /资源>
I want to change text color of Radio Button if clicked by user. However I can change background color but I don't want that..and can anyone even tell how to put cross or right sign as per right or wrong choice selected...? Here's my code for main class..
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
}
});
here's is my layout file:
<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>
解决方案
To change the text color of the radiobutton:
Red.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Red.setTextColor(Color.RED);
}
}
});
For custom icons; create a radiobutton.xml
in your drawable folder with this in it:
<?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>
And put these lines in your styles.xml
(in your values
folder):
<?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>
这篇关于更改单选按钮的文本颜色点击在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文