赞
踩
——各类控件的使用(控件)-单选框
【若对该知识点有更多想了解的,欢迎私信博主~~】
XML样式 | 说明 |
---|---|
layout_width | 布局宽度 |
layout_height | 布局高度 |
text | 文本值 |
textColor | 字体颜色 |
textSize | 字体大小 |
textStyle | 字体风格 |
buttonTint | 按钮色调 |
background | 背景 |
scaleX | X轴拉伸比例 |
scaleY | Y轴拉伸比例 |
checkedButton | 默认选中(填入id) |
checked | true为选中 |
button | @null时只显示文字 |
常用方法 | 说明 |
---|---|
isChecked() | 被选中 |
getText() | 获取单选框的值 |
RadioGroup rg;
RadioButton rb,rb1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main26);
//绑定控件
rg=findViewById(R.id.rg);
rb=findViewById(R.id.rb);
rb1=findViewById(R.id.rb1);
//改变选项触发
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.rb:
Toast.makeText(Main26Activity.this, ""+rb.getText().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.rb1:
Toast.makeText(Main26Activity.this, ""+rb1.getText().toString(), Toast.LENGTH_SHORT).show();
break;
}
}
});
}
圆圈的颜色(此文件名为radiobtn_circlecolor.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:color="#0000ff"/>
<item android:state_checked="false"
android:color="#ff0000"/>
</selector>
点击时的波纹颜色(此文件名为radiobtn_ripplecolor.xml)
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#0000ff"
tools:targetApi="lollipop">
</ripple>
文字的颜色(此文件名为radiobtn_textcolor.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:color="#0000ff"/>
<item android:state_checked="false"
android:color="#ff0000"/>
</selector>
<RadioButton
android:layout_width="100dp"
android:layout_height="80dp"
android:text="男"
android:textSize="30sp"
android:buttonTint="@drawable/radiobtn_circlecolor"
android:background="@drawable/radiobtn_ripplecolor"
android:textColor="@drawable/radiobtn_textcolor"
android:textStyle="bold"
android:scaleX="1.5"
android:scaleY="1.5"
android:id="@+id/rb"/>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。