Checkbox复选框实验

本文介绍了一个使用CheckBox组件的应用案例,展示了如何创建UI界面布局、编写相关代码,并实现用户交互功能,如监听复选框状态变化及按钮点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原创 灵思致远 2018-05-16


1. 实验内容简介

多项选择(CheckBox)组件也被称为复选框,该组件通常用于某选项的打开或关闭。CheckBox表明一个特定的状态是勾选(on,值为1)还是不勾选(off,值为0),在应用程序中为用户提供“真”或“假”选择。复选框状态彼此独立,因此可同时选择任意多个。

通过鼠标单击复选框,可触发复选框状态的改变。复选框会从当前状态变到另一种状态。

使用步骤:

步骤1:声明CheckBox变量

步骤2:通过FindViewById关联或绑定

步骤3:监听用户输入动作

2. UI界面布局


<RelativeLayout  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"

    tools:context="${relativePackage}.${activityClass}">

 

    <TextView

        android:id="@+id/textView1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

        android:text="@string/Title"/>

 

    <CheckBox

        android:id="@+id/checkBox1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentLeft="true"

       android:layout_below="@+id/textView1"

       android:text="@string/mz_balin" />

 

    <CheckBox

        android:id="@+id/checkBox2"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentLeft="true"

       android:layout_below="@+id/checkBox1"

       android:text="@string/mz_buyi" />

 

    <CheckBox

        android:id="@+id/checkBox3"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentLeft="true"

       android:layout_below="@+id/checkBox2"

        android:text="@string/mz_gaoshan"/>

 

    <CheckBox

        android:id="@+id/checkBox4"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentLeft="true"

       android:layout_below="@+id/checkBox3"

        android:text="@string/mz_shezu"/>

 

    <Button

        android:id="@+id/button1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentLeft="true"

        android:layout_below="@+id/checkBox4"

       android:layout_marginLeft="24dp"

        android:text="@string/Submit"/>

 

</RelativeLayout>

 在res->values->string.xml加入:

<?xmlversion="1.0" encoding="utf-8"?>

<resources>

 

    <string name="app_name">CheckBoxDemo</string>

    <string name="hello_world">Hello world!</string>

    <string name="Title">下列属于中国的56个民族的是哪些?</string>

    <string name="mz_gaoshan">高山族</string>

    <string name="mz_shezu">畲族</string>

    <string name="mz_buyi">布依族</string>

    <string name="mz_balin">巴林族</string>

    <string name="Submit">提交</string>

</resources>

 

3. 代码编写和调试

public  class MainActivity extends Activity {

    CheckBox checkbox1;

    CheckBox checkbox2;

    CheckBox checkbox3;

    CheckBox checkbox4;

    Button button;

    @Override

    protected void onCreate(BundlesavedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        checkbox1 = (CheckBox)findViewById(R.id.checkBox1);

        checkbox2 = (CheckBox)findViewById(R.id.checkBox2);

        checkbox3 = (CheckBox)findViewById(R.id.checkBox3);

        checkbox4 = (CheckBox) findViewById(R.id.checkBox4);

 

        checkbox1.setOnCheckedChangeListener(new CheckBoxListener());

        checkbox2.setOnCheckedChangeListener(new CheckBoxListener());

        checkbox3.setOnCheckedChangeListener(new CheckBoxListener());

        checkbox4.setOnCheckedChangeListener(new CheckBoxListener());

 

        button = (Button)findViewById(R.id.button1);

        button.setOnClickListener(new  OnClickListener() {

 

            @Override

            public  void  onClick(View v) {

                if(checkbox2.isChecked()&&checkbox3.isChecked() &&checkbox4.isChecked() )

                {

                    Toast.makeText(MainActivity.this,"答案正确", Toast.LENGTH_SHORT).show();

                }

                else

                {

                   Toast.makeText(MainActivity.this,"答案错误", Toast.LENGTH_SHORT).show();

                }

 

            }

        });

    }

    class CheckBoxListener implementsOnCheckedChangeListener {

        public void  onCheckedChanged(CompoundButton buttonView,

                boolean isChecked) {

            if(isChecked)

            {

                Toast.makeText(MainActivity.this,"被选择", Toast.LENGTH_SHORT).show();

            }

            else

            {

                Toast.makeText(MainActivity.this,"取消选择", Toast.LENGTH_SHORT).show();

            }

 

        }

    }

 

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值