第12章:单选扭(RadioButton)和复选框(CheckBox)的功能和用法

来源:互联网 发布:php网页聊天室原理 编辑:程序博客网 时间:2024/06/04 21:18

今天主要讲的就是RadioButton和CheckBox的用法。

首先看名字就知道它们肯定是继承了Button类,可以使用Button的各种属性和方法。但是他们又多了个可以选择的功能。

RadioButton是单选按钮,也就是说一组RadioButton只能选择一个,所以 ,一组RadioButton写在一个RadioGroup下面,CheckBox是多选按钮,所以,每个CheckBox都要单独写。下面我们以一个实例来看看他们的用法和功能。

这里建立的唯一标识是cn.edu.hpu.acm.checkButtonTest

string.xml

<resources>    <string name="app_name">单选、多选框</string>    <string name="hello_world">Hello world!</string>    <string name="action_settings">Settings</string>    <string name="sex">性别</string>    <string name="male">男</string>    <string name="female">女</string>    <string name="likeColor">喜欢的颜色</string>    <string name="red">红色</string>    <string name="blue">蓝色</string>    <string name="green">绿色</string>    <string name="result1">你的性别是女人</string>    <string name="result2">你的性别是男人</string>    <string name="redResult">你喜欢红色</string>    <string name="blueResult">你喜欢蓝色</string>    <string name="greenResult">你喜欢绿色</string>    <string name="title_activity_other">OtherActivity</string></resources>

activity_main.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">    <TableRow>        <TextView             android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="@string/sex"/>        <RadioGroup            android:id="@+id/rg">            <RadioButton                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/male"                android:text="@string/male"/>            <RadioButton                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/female"                android:text="@string/female"/>        </RadioGroup>    </TableRow>    <TableRow>        <TextView            android:layout_height="wrap_content"            android:layout_width="match_parent"            android:text="@string/likeColor"/>        <LinearLayout            android:orientation="vertical"            android:layout_height="wrap_content"            android:layout_width="wrap_content">            <CheckBox                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/red"                android:text="@string/red"/>            <CheckBox                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/blue"                android:text="@string/blue"/>            <CheckBox                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/green"                android:text="@string/green"/>        </LinearLayout>    </TableRow>    <TextView        android:layout_height="wrap_content"        android:layout_width="match_parent"        android:id="@+id/show"/></TableLayout>

布局截图


以上已经实现了主要功能,最后一个按钮写的是跳转,没什么实际意义,可以带值跳过去,不过意义不大,并且那个布局及Java代码都没几行,所以就看以上就可以了。

0 0
原创粉丝点击