android學習筆記12:RadioButton和checkbox

來源:互聯網
上載者:User

radiobutton即選項按鈕,checkbox即複選按鈕也就是可以同時選擇多個選項。下面的程式定義了一組選項按鈕和三個複選框,來讓使用者選擇使用者相關的資訊,其中選項按鈕必須放在一個RadioGroup中才能實現單選的效果。

 

 

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TableRow> 
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="性別:" 
    android:textSize="11pt" 
    /> 
<!-- 定義一組單選框 --> 
<RadioGroup  
    android:orientation="horizontal" 
    android:layout_gravity="center_horizontal"> 
<!-- 定義兩個單選框 --> 
<RadioButton android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="男" 
    /> 
<RadioButton android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="女" 
    /> 
</RadioGroup> 
</TableRow>    
<TableRow> 
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="喜歡的顏色:" 
    android:textSize="11pt" 
    /> 
<!-- 定義一個垂直的線性布局 --> 
<LinearLayout android:layout_gravity="center_horizontal" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

<!-- 定義三個複選框 --> 
<CheckBox android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="紅色" 
    android:checked="true" 
/> 
<CheckBox android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="藍色" 
/> 
<CheckBox android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="綠色" 
/> 
</LinearLayout> 
</TableRow> 
</TableLayout> 
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<TableRow>
<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="性別:"
 android:textSize="11pt"
 />
<!-- 定義一組單選框 -->
<RadioGroup
 android:orientation="horizontal"
 android:layout_gravity="center_horizontal">
<!-- 定義兩個單選框 -->
<RadioButton android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="男"
 />
<RadioButton android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="女"
 />
</RadioGroup>
</TableRow> 
<TableRow>
<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="喜歡的顏色:"
 android:textSize="11pt"
 />
<!-- 定義一個垂直的線性布局 -->
<LinearLayout android:layout_gravity="center_horizontal"
 android:orientation="vertical"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
>
<!-- 定義三個複選框 -->
<CheckBox android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="紅色"
 android:checked="true"
/>
<CheckBox android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="藍色"
/>
<CheckBox android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="綠色"
/>
</LinearLayout>
</TableRow>
</TableLayout>

 

摘自 hn307165411的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.