Android:复选框CheckBox

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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"/>

    <CheckBox
        android:id="@+id/seeBook"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="看书"
        />
    <CheckBox
        android:id="@+id/write"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写字"
        />
    <CheckBox
        android:id="@+id/code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="敲代码"
        />
    <CheckBox
        android:id="@+id/seeBeauty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="看美女"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确认"
        android:id="@+id/submit"
        />

</LinearLayout>
 

MainActivity .java:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {

    //定义复选框
    private CheckBox seeBook, write, code , seeBeauty;

    // 定义按钮
    private Button button;

    //定义一个集合存放用户选择了哪几个爱好
    private List<String> result = new ArrayList<>();


    //定义复选框点击事件对象
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            /**
             * 第一个参数CompoundButton: 代表用户当前点击的复选框按钮
             * 参数二 isChecked: 是否选中true,false
             *
             * **/
            //选中
            if(isChecked){
                //把 选中的内容添加到集合
                result.add((String) buttonView.getText());
            }  else {//否则 把内容删掉
                result.remove((String) buttonView.getText());
            }
         }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获取复选框对象
        seeBook = findViewById(R.id.seeBook);
        write = findViewById(R.id.write);
        code = findViewById(R.id.code);
        seeBeauty = findViewById(R.id.seeBeauty);

        // 确定按钮
        button = findViewById(R.id.submit);
        //绑定点击监听事件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //弹出框 选中的内容
                Toast.makeText(MainActivity.this,result.toString(),Toast.LENGTH_LONG).show();
            }
        });

        //绑定点击复选框监听事件
        seeBook.setOnCheckedChangeListener(onCheckedChangeListener);
        write.setOnCheckedChangeListener(onCheckedChangeListener);
        code.setOnCheckedChangeListener(onCheckedChangeListener);
        seeBeauty.setOnCheckedChangeListener(onCheckedChangeListener);
    }

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

javaGHui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值