关于checkbox的监听 多选

本文介绍了一个简单的Android应用案例,展示了如何使用CheckBox控件并为它们设置监听器来响应用户的操作。通过具体的代码示例,解释了如何利用onCheckedChanged方法捕获CheckBox的状态变化,并根据这些变化来执行相应的操作。

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

 CheckBox cb_remember=(CheckBox)findViewById(R.id.cb_remember);

 cb_remember.setOnCheckedChangeListener(new OnCheckedChangeListener() {   

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  

int id=buttonView.getId();  

Toast.makeText(getApplicationContext(), "123:"+id , Toast.LENGTH_LONG.show();  

}});

Toast显示的是     123:2131165489  所以说 buttonView 通常用于获取某个checkbox的id  可用于多选。 isChecked用于确定是否被选中

<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="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="多选" />
    <CheckBox 
        android:id="@+id/check1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="爱吃苹果"
        />
     <CheckBox 
        android:id="@+id/check2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="爱吃橡胶"
        />
      <CheckBox 
        android:id="@+id/check3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="爱吃便便"
        />

</LinearLayout>

package com.wang.checkbox;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {

	private CheckBox check2, check3, check1;

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

		check2 = (CheckBox) findViewById(R.id.check2);
		check1 = (CheckBox) findViewById(R.id.check1);
		check3 = (CheckBox) findViewById(R.id.check3);

		check2.setOnCheckedChangeListener(listener);
		check1.setOnCheckedChangeListener(listener);
		check3.setOnCheckedChangeListener(listener);

	}

	private OnCheckedChangeListener listener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			switch (buttonView.getId()) {
			case R.id.check1:
				Toast.makeText(getApplication(), "爱吃苹果", 0).show();
				break;

			case R.id.check2:
				Toast.makeText(getApplication(), "爱吃橡胶", 0).show();
				break;
			case R.id.check3:
				Toast.makeText(getApplication(), "爱吃便便", 0).show();
				break;
			default:
				break;
			}

		}
	};

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值