Android开发——控件基础(五)RadioButton组件:属性、⽰
例代码
实现界⾯:
常⽤属性:
android:text 设置选项⽂本
android:gravity="center" 设置居中
android:button="@null" 设置⽆按钮,配合⾃定义背景使⽤
android:textSize 设置⽂本字体⼤⼩
android:checked="true" 设置默认选中
android:background 设置⾃定义背景
android:textColor 设置⽂本颜⾊
代码实现:
activity_radio_button:布局⽂件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="schemas.android/apk/res/android"
xmlns:tools="schemas.android/tools"
xmlns:tools="schemas.android/tools"
android:id="@+id/activity_radio_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ise.RadioButtonActivity"> <!--配置单选按钮字体颜⾊、⼤⼩,选中-->
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rg_1"
android:orientation="horizontal">
<RadioButton
htmlradio设置默认的按钮android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textSize="16sp"
android:checked="true"
android:textColor="@color/colorDeepBlue"
android:id="@+id/rb_1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="⼥"
android:textSize="16sp"
android:textColor="@color/colorDeepBlue"
android:id="@+id/rb_2"/>
</RadioGroup>
<!--⾃定义单选按钮背景形状-->
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rg_2"
android:orientation="vertical"
android:layout_below="@+id/rg_1"
android:layout_marginTop="30dp">
<RadioButton
android:layout_width="60dp"
android:layout_height="30dp"
android:text="上"
android:gravity="center"
android:button="@null"
android:textSize="16sp"
android:checked="true"
android:background="@drawable/selector_orange_radiobutton"
android:textColor="@color/colorDeepBlue"
android:id="@+id/rb_3"/>
<RadioButton
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:text="下"
android:background="@drawable/selector_orange_radiobutton"
android:gravity="center"
android:button="@null"
android:textSize="16sp"
android:textColor="@color/colorDeepBlue"
android:id="@+id/rb_4"/>
</RadioGroup>
</RelativeLayout>
RadioButtonActivity:java⽂件
ise;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class RadioButtonActivity extends AppCompatActivity {
RadioGroup radioGroup,radioGroup1; //定义单选按钮组
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_radio_button);
radioGroup = (RadioGroup) findViewById(_1); //获取单选按钮组
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { //配置选中改变
@Override
//属性
//第⼀个属性:点击的单选按钮组
//第⼆个属性:点击的单选按钮序号
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton = (RadioButton) radioGroup.findViewById(i); //获取单选按钮
Toast.makeText(RadioButtonActivity.Text(),Toast.LENGTH_SHORT).show(); //显⽰单选按钮信息 }
});
radioGroup1 = (RadioGroup) findViewById(_2); //获取单选按钮组
radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { //配置选中改变
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton = (RadioButton) radioGroup.findViewById(i); //获取单选按钮
Toast.makeText(RadioButtonActivity.Text(),Toast.LENGTH_SHORT).show(); //显⽰单选按钮信息 }
});
}
}
drawable⽂件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="schemas.android/apk/res/android">
<item android:state_checked="true">
<shape>
<solid android:color="@color/colorOrange" />
<corners android:radius="5dp"/>
</shape>
</item>
<item android:state_checked="false">
<shape>
<stroke android:width="1dp"
android:color="@color/colorOrange"/>
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论