您的位置:首页 > 移动开发 > Android开发

Android CheckBox和RadioButton

2015-12-04 20:27 501 查看

CheckBox概述:

Checkbox(多选框)可以让用户从一系列选项里选择一个或者多个选项. 比较典型的是像下图这样在一个垂直列表里选择可选项:



创建checkbox需要在layout文件中添加<CheckBox>标签, 因为可以多选, 所以需要为每个checkbox添加一个监听事件.

 

处理点击事件:

当用户选择一个checkbox的时候, CheckBox对象会收到一个点击事件, 跟Button一样, 我们可以通过android:onClick属性来为checkbox指定一个处理点击事件的方法. 比如这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <CheckBox android:id="@+id/checkbox_meat"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/meat"

        android:onClick="onCheckboxClicked"/>

    <CheckBox android:id="@+id/checkbox_cheese"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/cheese"

        android:onClick="onCheckboxClicked"/>
</LinearLayout>

然后我们可以在Activity中实现onCheckboxClicked()方法来处理checkbox的点击事件:

public
void onCheckboxClicked(View view)
{

    // Is the view now checked?

    boolean checked
= ((CheckBox) view).isChecked();

   

    // Check which checkbox was clicked

    switch(view.getId())
{

        case R.id.checkbox_meat:

            if (checked)

                // Put some meaton the sandwich

            else

                // Remove themeat

            break;

        case R.id.checkbox_cheese:

            if (checked)

                // Cheese me

            else

                // I'm lactoseintolerant

            break;

        // TODO: Veggie sandwich

    }
}

跟button的onClick方法一样, 需要满足几个条件: 必须是public方法, 必须返回void, 拥有一个唯一的View参数.

如果我们需要通过代码修改checkbox的状态(必须要加载一个保存的CheckBoxPreference), 我们可以使用setChecked(Boolean)或者toggle()方法.

 

RadioButton:

Radio button(单选框)允许用户从多个选项中选择一个, 如果在选择的时候需要用户看到所有的选项, 那么我们要使用RadioButton, 如果不需要, 那么我们应该使用一个spinner(下拉框). 单选框长这样:



我们可以通过在layout文件中声明<RadioButton>标签来添加一个RadioButton, 然而因为RadioButton是互相排斥的, 每组中只能选择一个, 所以我们必须将它们分组, 分组需要使用<RadioGroup>标签.

 

处理RadioButton的点击事件:

当用户点击RadioButton的时候, RadioButton对象会收到一个点击事件, 像Button和CheckBox一样, 我们也可以使用android:onClick属性来指定一个方法来响应RadioButton的点击事件, 栗子:

在layout文件中声明RadioGroup和RadioButton:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:orientation="vertical">

    <RadioButton android:id="@+id/radio_pirates"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/pirates"

        android:onClick="onRadioButtonClicked"/>

    <RadioButton android:id="@+id/radio_ninjas"

        android:layout_width="wrap_content"

        android:layout_h
b1f4
eight="wrap_content"

        android:text="@string/ninjas"

        android:onClick="onRadioButtonClicked"/>
</RadioGroup>

RadioGroup是LinearLayout的自己, 默认情况就拥有垂直方向的属性.

然后我们在Activity中定义处理事件的方法:

public
voidonRadioButtonClicked(View view)
{

    // Is the button now checked?

    boolean checked
= ((RadioButton) view).isChecked();

   

    // Check which radio button was clicked

    switch(view.getId())
{

        case R.id.radio_pirates:

            if (checked)

                // Pirates arethe best

            break;

        case R.id.radio_ninjas:

            if (checked)

                // Ninjas rule

            break;

    }
}

跟Button和CheckBox的处理方法一样, 这些方法需要满足: 必须是public方法, 必须返回void, 必须有且仅有一个View参数.

如果我们需要在代码中改变RadioButton本身的状态, 可以使用setChecked(boolean)或者toggle()方法.

参考: http://developer.android.com/guide/topics/ui/controls/radiobutton.html
http://developer.android.com/guide/topics/ui/controls/checkbox.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息