以编程方式更改单选按钮的背景 [英] Change background for radio button programatically
本文介绍了以编程方式更改单选按钮的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有两个RadioButton的 RadioGroup
。我想在禁用它们时以编程方式更改它们的颜色。
< RadioGroup
android:id = @ + id / radio_group_transfer
android:layout_width = match_parent
android:layout_height = wrap_content
android:layout_marginBottom = 22dp
android:layout_marginTop = 4dp
android:gravity = center_vertical
android:layoutDirection = ltr
android:orientation = horizontal>
< RadioButton
android:id = @ + id / national_id_docs
android:layout_width = match_parent
android:layout_height = wrap_content
android:layout_weight = 1
android:background = @ drawable / radio_btn_selector_left
android:button = @ null
android:checked = true
android:gravity = center
android:padding = 10dp
android:text = @ string / national_id
android:textColor = @ drawable / radio_btn_textcolor_selector
style = @ style / SimpleTextStyleSemiBold />
< RadioButton
android:id = @ + id / passport
android:layout_width = match_parent
android:layout_height = wrap_content
android:layout_weight = 1
android:background = @ drawable / radio_btn_selector_right
android:button = @ null
android:gravity = center
android:padding = 10dp
android:text = @ string / passport
android:textColor = @ drawable / radio_btn_textcolor_selector
style = @ style / SimpleTextStyleSemiBold />
< / RadioGroup>
我用灰色创建了新的XML选择器,但是当我设置背景时,它不起作用。 / p>
radio_btn_selector_left_disabled.xml
< ?xml version = 1.0 encoding = utf-8?>
<选择器xmlns:android = http://schemas.android.com/apk/res/android>
< item android:state_checked = true>
< shape>
< solid android:color = @ color / employee_non_editable_color_grey />
<描边android:width = 1dp android:color = @ color / employee_non_editable_color_grey />
< corners android:bottomLeftRadius = 3dp android:topLeftRadius = 3dp />
< / shape>
< / item>
< item android:state_checked = false>
< shape android:shape = rectangle>
< solid android:color =#ffffff />
<描边android:width = 1dp android:color = @ color / employee_non_editable_color_grey />
< corners android:bottomLeftRadius = 3dp android:topLeftRadius = 3dp />
< / shape>
< / item>
< / selector>
我尝试过
national_id_docs.setBackground(R.drawable.radio_btn_selector_left_disabled);
您能否建议使用 radio_btn_selector_left_disabled $的最佳方法是什么? c $ c>是单选按钮?
解决方案
final RadioGroup rg =(RadioGroup) findViewById(R.id.radio_group_transfer);
final RadioButton national =(RadioButton)findViewById(R.id.national_id_docs);
最终RadioButton护照=(RadioButton)findViewById(R.id.passport);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group,intcheckedId){
if(national.isChecked( )){
national.setTextColor(Color.BLUE);
national.setBackgroundColor(Color.BLUE);
passport.setTextColor(Color.BLACK);
password.setBackgroundColor(Color.BLACK);
}
if(passport.isChecked()){
passport.setTextColor(Color.BLUE);
passport.setBackgroundColor(Color。 BLUE);
national.setTextColor(Color.BLACK);
national.setBackgroundColor(Color.BLACK);
}
}
});
I have a RadioGroup
with two RadioButtons. I want to change the color of them programmatically when they are disabled.
<RadioGroup
android:id="@+id/radio_group_transfer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="22dp"
android:layout_marginTop="4dp"
android:gravity="center_vertical"
android:layoutDirection="ltr"
android:orientation="horizontal">
<RadioButton
android:id="@+id/national_id_docs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/radio_btn_selector_left"
android:button="@null"
android:checked="true"
android:gravity="center"
android:padding="10dp"
android:text="@string/national_id"
android:textColor="@drawable/radio_btn_textcolor_selector"
style="@style/SimpleTextStyleSemiBold"/>
<RadioButton
android:id="@+id/passport"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/radio_btn_selector_right"
android:button="@null"
android:gravity="center"
android:padding="10dp"
android:text="@string/passport"
android:textColor="@drawable/radio_btn_textcolor_selector"
style="@style/SimpleTextStyleSemiBold"/>
</RadioGroup>
I have created new XML selectors with gray colors but when I set the background it does not work.
radio_btn_selector_left_disabled.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
<solid android:color="@color/employee_non_editable_color_grey" />
<stroke android:width="1dp" android:color="@color/employee_non_editable_color_grey" />
<corners android:bottomLeftRadius="3dp" android:topLeftRadius="3dp"/>
</shape>
</item>
<item android:state_checked="false">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="@color/employee_non_editable_color_grey" />
<corners android:bottomLeftRadius="3dp" android:topLeftRadius="3dp"/>
</shape>
</item>
</selector>
I tried
national_id_docs.setBackground(R.drawable.radio_btn_selector_left_disabled);
could you suggest what is the best way to use the radio_btn_selector_left_disabled
for the radio button?
解决方案
final RadioGroup rg = (RadioGroup) findViewById(R.id.radio_group_transfer);
final RadioButton national = (RadioButton) findViewById(R.id.national_id_docs);
final RadioButton passport = (RadioButton) findViewById(R.id.passport);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (national.isChecked()) {
national.setTextColor(Color.BLUE);
national.setBackgroundColor(Color.BLUE);
passport.setTextColor(Color.BLACK);
passport.setBackgroundColor(Color.BLACK);
}
if (passport.isChecked()){
passport.setTextColor(Color.BLUE);
passport.setBackgroundColor(Color.BLUE);
national.setTextColor(Color.BLACK);
national.setBackgroundColor(Color.BLACK);
}
}
});
这篇关于以编程方式更改单选按钮的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文