Android的CheckBox复选框

代码如下

<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"
     >
    <CheckBox 
        android:id="@+id/onecheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学习"
        />
 <CheckBox 
        android:id="@+id/twocheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="睡觉"
        />
   <CheckBox 
        android:id="@+id/therecheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="吃饭"
        />
   <CheckBox 
        android:id="@+id/allcheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打代码"
        />
     

</LinearLayout>

package com.example.tectcheck;

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

public class MainActivity extends Activity {

	private CheckBox onecheck;
     private CheckBox twocheck;
     private CheckBox therecheck;
     private CheckBox allcheck;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		onecheck=(CheckBox)findViewById(R.id.onecheck);
		twocheck=(CheckBox)findViewById(R.id.twocheck);
		therecheck=(CheckBox)findViewById(R.id.therecheck);
		allcheck=(CheckBox)findViewById(R.id.allcheck);
		
		AllCheckListener listener=new AllCheckListener();
		allcheck.setOnCheckedChangeListener(listener);
		
		
	}
	class AllCheckListener implements OnCheckedChangeListener{

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// 第一种方法
//			if(isChecked){
//				onecheck.setChecked(true);
//				twocheck.setChecked(true);
//				therecheck.setChecked(true);
//			}else{
//				onecheck.setChecked(false);
//				twocheck.setChecked(false);
//				therecheck.setChecked(false);
//			}
		//第二种方法
			onecheck.setChecked(isChecked);
			twocheck.setChecked(isChecked);
			therecheck.setChecked(isChecked);
			
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

效果图


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值