android----checkbox

本文介绍了一个使用Android开发的示例,展示了如何通过CheckBox组件实现城市选择功能,并利用List记录选中状态。具体实现了北京、上海和深圳三个城市的CheckBox监听器,当用户勾选或取消勾选时,会在日志中打印当前选中的城市列表。

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

activity

package com.androidless1.yifei.andrlidl1;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;

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

public class MainActivity extends AppCompatActivity {

    public static final String TAG = "MainActivity" + 123;
    private  int [] ch =new int[3];
    private CheckBox beijing;
    private CheckBox shanghai;
    private CheckBox shenzhen;
    private List<Integer> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        beijing = findViewById(R.id.beginning);
        shanghai = findViewById(R.id.shanghai);
        shenzhen = findViewById(R.id.shenzhen);
        beijing.setOnCheckedChangeListener(new CheckLister());
        shanghai.setOnCheckedChangeListener(new CheckLister());
        shenzhen.setOnCheckedChangeListener(new CheckLister());
    }

    class CheckLister implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (buttonView.getId()) {
                case R.id.beginning:
                    if(isChecked){
                        if(!list.contains(R.id.beginning)){
                            list.add(R.id.beginning);
                        }

                    }else {
                        if(list.contains(R.id.beginning)){
                            list.remove(list.indexOf(R.id.beginning));
                        }
                    }
                    Log.d(TAG, "onCheckedChanged: "+list);
                    break;
                case R.id.shanghai:
                    if(isChecked){
                        if(!list.contains(R.id.shanghai)){
                            list.add(R.id.shanghai);
                        }

                    }else {
                        if(list.contains(R.id.shanghai)){
                            list.remove(list.indexOf(R.id.shanghai));
                        }
                    }
                    Log.d(TAG, "onCheckedChanged: "+list);
                    break;
                case R.id.shenzhen:
                    if(isChecked){
                        if(!list.contains(R.id.shenzhen)){
                            list.add(R.id.shenzhen);
                        }

                    }else {
                        if(list.contains(R.id.shenzhen)){
                            list.remove(list.indexOf(R.id.shenzhen));
                        }
                    }
                    Log.d(TAG, "onCheckedChanged: "+list);
                    break;
                default:
                    return;
            }

        }
    }
}








xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:id="@+id/beginning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="北京"
        />
    <CheckBox
        android:id="@+id/shanghai"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上海"
        />
    <CheckBox
        android:id="@+id/shenzhen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="深圳"
        />
</LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值