按鈕組的用處在哪呢?當然是選擇性別的時候,當我們選擇男和女的時候,我們只能選擇一個,
下面定義了兩個按鈕組,每個按鈕組分別有包含兩個按鈕,為了能夠不重復選擇兩個,必須將他們放在一個按鈕組里,
第一個按鈕組,采用比較普通的兩個按鈕,可以選擇,通過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>
運行結果為:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/244311.html
標籤:其他
