在AS中checkbox事件处理的的各种方法,向大家分享我的做法

首先,写好自己需要的布局的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/Lear">
    <CheckBox
        android:id="@+id/box1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打羽毛球"
        android:textSize="18sp"/>
    <CheckBox
        android:id="@+id/box2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打篮球"
        android:textSize="18sp"/>
    <CheckBox
        android:id="@+id/box3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打乒乓球"
        android:textSize="18sp"/>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:onClick="button"
        android:textSize="18sp"/>
</LinearLayout>

在这里插入图片描述
当然复选框中的文字可以自己选择
复选框的处理事件我写了很多种

package com.example.myandroidcheckbox;

import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity implements OnCheckedChangeListener {
   // public TextView text;
    public CheckBox CB1,CB2,CB3;     //implements OnCheckedChangeListener
    public Button btn1;
    public String hobbys;
    LinearLayout Lin;
    List<String> List= new ArrayList<String>();//集合


    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CheckBox  CB1=(CheckBox)findViewById(R.id.box1);
        CheckBox  CB2=(CheckBox)findViewById(R.id.box2);
        CheckBox  CB3=(CheckBox)findViewById(R.id.box3);
        btn1=(Button)findViewById(R.id.btn);
        CB1.setOnCheckedChangeListener(this);
        CB2.setOnCheckedChangeListener(this);
        CB3.setOnCheckedChangeListener(this);
        Lin = (LinearLayout)findViewById(R.id.Lear);
        hobbys=new String();
    }
    //这一部分是点击复选框它就会显示你所选的文字
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
        String motion=buttonView.getText().toString();
        if(isChecked){
            if(!hobbys.contains(motion)){
                hobbys=hobbys+motion;
                Toast.makeText(MainActivity.this,hobbys,Toast.LENGTH_LONG).show();
            }
        }else {
            if(hobbys.contains(motion)){
                hobbys=hobbys.replace(motion,"");
                Toast.makeText(MainActivity.this,hobbys,Toast.LENGTH_LONG).show();
            }
        }
    }

下面这段代码是按钮处理事件可以加到下面
点击按钮它会利用Toast显示在屏幕上

public void button(View view){
    int CheckLenth = Lin.getChildCount()-1;//获取子控件数
    for(int i=0;i<CheckLenth;i++){
        CheckBox BX = (CheckBox)Lin.getChildAt(i);//判断有没有被选中
        if(BX.isChecked()){
            List.add(BX.getText().toString());
        }
    }
    if(List.isEmpty()){
        Toast.makeText(MainActivity.this,"请选择兴趣爱好",Toast.LENGTH_SHORT).show();

    }
    else {
        Toast.makeText(MainActivity.this, List.toString().substring(1, List.toString().length() - 1), Toast.LENGTH_SHORT).show();
    }
    List.clear();
}

后面将为大将讲解一下代码
int CheckLenth = Lin.getChildCount()-1;//获取子控件数
Lin是LinearLayout布局创建的对象通过getChildrenCount来获取布局中子控件的个数
List List= new ArrayList();//集合这个是集合的定义

(当然可以用stringbuffer,或者字符串数组都可以)主要是用来存放选中的复选框的文本
for(int i=0;i<CheckLenth;i++){
CheckBox BX = (CheckBox)Lin.getChildAt(i);//判断有没有被选中
if(BX.isChecked()){
List.add(BX.getText().toString());
}
}
这一段判断有没有被选中将选中复选框的文本加入到集合当中然后输出到屏幕
再给你们讲解一下Toast,很多人对这个肯定不熟悉
Toast是安卓系统提供轻量级信息提醒机制,用于向用户提示即时消息。它显示在应用程序界面最上层,显示一段时间自动消失不会打断当前操作,也捕获的焦点。
使用Toast.makeText(Context,Text,Time).show();
Context:表示程序环境的信息,即当前组件的上下文环境。如果在Activity中使用Toast提示信息,该参数可以设置为Activity.this
Text表示提示的字符串消息。
Time:表示显示时长,其属性值包括Toast.LENGTH_SHOR和ToastLENGTH_LONG,分别表示较短时间和较长时间。
结果
如下图所示。

在这里插入图片描述
再分享一些其他的处理方式
Button myBtn;
CheckBox check1,check2,check3;
List list = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myBtn = (Button)findViewById(R.id.submitBtn);
check1 = (CheckBox)findViewById(R.id.run);
check2 = (CheckBox)findViewById(R.id.swim);
check3 = (CheckBox)findViewById(R.id.badminton)
内部类的方式用的lamda表达式
复选框的处理方法
check1.setOnCheckedChangeListener((buttonView, isChecked) ->{
if(isChecked){
list.add(“跑步”);
Toast.makeText(MainActivity.this,“跑步被选中”,Toast.LENGTH_SHORT).show();
}else {
list.remove(“跑步”);
}
});
按钮处理的方法
myBtn.setOnClickListener((v) ->{
if(list.isEmpty()){
Toast.makeText(MainActivity.this,“你什么都没选中”,Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, list.toString().substring(1, list.toString().length() - 1), Toast.LENGTH_SHORT).show();
}
});
大家根据自己的想法选择怎么去写代码。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值