(七)Android中的RadioButton组件

按钮组的用处在哪呢?当然是选择性别的时候,当我们选择男和女的时候,我们只能选择一个。
下面定义了两个按钮组,每个按钮组分别有包含两个按钮,为了能够不重复选择两个,必须将他们放在一个按钮组里。
第一个按钮组,采用比较普通的两个按钮,可以选择,通过checked=“true”设置默认选择
第二个按钮组,则设计更加美观的格式,去除了按钮的原点,android:button=”@null",然后背景属性参照了按钮按压的不同效果那里,这里才用按钮是否被选择。

<RadioGroup
        android:id="@+id/radiogroupsex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="10dp"
        >
        <RadioButton
            android:id="@+id/radiobutton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#000000"
            android:textSize="16sp"
            android:checked="true"></RadioButton>

        <RadioButton
            android:id="@+id/radiobutton2"
            android:layout_width="wrap_content"
            android:layout_height="22dp"
            android:layout_marginTop="10dp"
            android:text=""
            android:textColor="#000000"
            android:textSize="16sp"></RadioButton>
    </RadioGroup>
    <RadioGroup
        android:id="@+id/radiogroupsex2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="30dp"
        android:layout_below="@id/radiogroupsex"
        >
        <RadioButton
            android:id="@+id/radiobutton3"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:gravity="center"
            android:text=""
            android:textColor="#000000"
            android:button="@null"
            android:textSize="18sp"
            android:checked="true"
            android:background="@drawable/buttongroupshape"></RadioButton>

        <RadioButton
            android:id="@+id/radiobutton4"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:gravity="center"
            android:text=""
            android:textColor="#000000"
            android:button="@null"
            android:textSize="18sp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/buttongroupshape"></RadioButton>
    </RadioGroup>

按钮组二设置背景的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="#CC33AE"></solid>
        <corners android:radius="50dp"></corners>
    </shape>
</item>
<item android:state_checked="false">
    <shape>
        <stroke android:color="#CC33AE"
            android:width="1dp"
            ></stroke>

        <corners android:radius="50dp"></corners>
    </shape>
</item>
</selector>

运行结果为:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/q54188p/article/details/112108116