• 多选按钮CheckBox


    main.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    <CheckBox android:id="@+id/eatId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="吃饭" /> <CheckBox android:id="@+id/sleepId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="睡觉" /> <CheckBox android:id="@+id/playDotaId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打dota" /> <CheckBox android:id="@+id/allCheckId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选" /> </LinearLayout>

     MainActivity.java:

    package com.example.allchecked;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    
    public class MainActivity extends Activity {
    	private CheckBox eatBox;
    	private CheckBox sleepBox;
    	private CheckBox playDotaBox;
    	private CheckBox allCheckBox;
    	
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		eatBox = (CheckBox)findViewById(R.id.eatId);
    		sleepBox = (CheckBox)findViewById(R.id.sleepId);
    		playDotaBox = (CheckBox)findViewById(R.id.playDotaId);
    		allCheckBox = (CheckBox)findViewById(R.id.allCheckId);
    		
    		
    		AllCheckListener allCheckListener = new AllCheckListener();
    		allCheckBox.setOnCheckedChangeListener(allCheckListener);
    		
    		CheckBoxListener listener = new  CheckBoxListener();
    		eatBox.setOnCheckedChangeListener(listener);
    		sleepBox.setOnCheckedChangeListener(listener);
    		playDotaBox.setOnCheckedChangeListener(listener);
    		
    /*		OnBoxClickListener clicklistener = new OnBoxClickListener();
    		eatBox.setOnClickListener(clicklistener);
    		sleepBox.setOnClickListener(clicklistener);
    		playDotaBox.setOnClickListener(clicklistener);
    */
    	}
    	
    	
    /*	class OnBoxClickListener implements OnClickListener{
    
    		@Override
    		public void onClick(View view) {
    			// TODO Auto-generated method stub
    			CheckBox box = (CheckBox)view;
    			if(box.getId() == R.id.eatId){
    				System.out.println("eatBox");
    			}
    			else if(box.getId() == R.id.sleepId){
    				System.out.println("sleepBox");
    			}
    			else{
    				System.out.println("dotaBox");
    			}
    			if(box.isChecked()){
    				System.out.println("checked");
    			}
    			else{
    				System.out.println("unchecked");
    			}
    		}
    	}*/
    
    	public class CheckBoxListener implements OnCheckedChangeListener{
    
    		@Override
    		public void onCheckedChanged(CompoundButton buttonView,
    				boolean isChecked) {
    			// TODO Auto-generated method stub
    			if(buttonView.getId() == R.id.eatId){
    				System.out.println("eatBox");
    			}
    			else if(buttonView.getId() == R.id.sleepId){
    				System.out.println("sleepBox");
    			}
    			else{
    				System.out.println("dotaBox");
    			}
    			if(isChecked){
    				System.out.println("checked");
    			}
    			else{
    				System.out.println("unchecked");
    			}
    		}
    		
    	}
    	
    	public class AllCheckListener implements OnCheckedChangeListener{
    
    		@Override
    		public void onCheckedChanged(CompoundButton buttonView,
    				boolean isChecked) {
    			// TODO Auto-generated method stub
    				eatBox.setChecked(isChecked);
    				sleepBox.setChecked(isChecked);
    				playDotaBox.setChecked(isChecked);
    		}
    		
    	}
    
    }
    

     全选按钮的功能还不是很完善,选中上面三个所有,全选按钮不会自动被选中;同时点击全选之后,点击上面某一个不再选中,全选不会自动取消选中。

  • 相关阅读:
    学习Python第五天
    学习Python第三天
    Mongodb
    JMS links
    笔记整理
    a* products
    String
    business knowledge
    [Java coding] leetcode notes
    [Java Basics3] XML, Unit testing
  • 原文地址:https://www.cnblogs.com/leihupqrst/p/3182698.html
Copyright © 2020-2023  润新知