Android控件之CheckBox多选框

Android中的CheckBox控件既可以通过Button按钮来监听其选中状态,它也有自己的事件处理方法,通过一个小例子来看具体代码

如下图:

这里写图片描述

这里写图片描述


1.0 activity_main.xml:

<LinearLayout 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:orientation="horizontal"
    android:gravity="center"
    tools:context="com.lgl.checkbox.MainActivity" >

    <CheckBox
        android:id="@+id/cb01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/huawei" />
    <CheckBox
        android:id="@+id/cb02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/iphone" />
    <CheckBox
        android:id="@+id/cb03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/xiaomi" />

    <Button
        android:id="@+id/btn01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/click" />

</LinearLayout>

2.0 MainActivity:

package com.lgl.checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {

    private CheckBox ch01;
    private CheckBox ch02;
    private CheckBox ch03;
    private Button btn01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         * 获取控件
         */
        ch01 = (CheckBox) findViewById(R.id.cb01);
        ch02 = (CheckBox) findViewById(R.id.cb02);
        ch03 = (CheckBox) findViewById(R.id.cb03);
        btn01 = (Button) findViewById(R.id.btn01);

        /**
         * 为三个CheckBox分别绑定监听
         */
        ch01.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                String text = ch01.getText().toString();
                Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });
        ch02.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                String text = ch02.getText().toString();
                Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });
        ch03.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                String text = ch03.getText().toString();
                Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });

        /**
         * 为Button绑定监听
         */
        btn01.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                String text = "您选择了:";
                if(ch01.isChecked()) {
                    text += ch01.getText().toString();
                }
                if(ch02.isChecked()) {
                    text += ch02.getText().toString();
                }
                if(ch03.isChecked()) {
                    text += ch03.getText().toString();
                }
                Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });

    }

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值