Android学习笔记——多选按钮CheckBox

 

实现多选按钮的监听有两种方法:OnClickListener 和 OnCheckedChangeListener

下面是这两种方法的实现:

  1. OnClickListener 的使用方法

     activity_main部分

<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" >

    <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/playId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="玩耍"/>


</LinearLayout>
activity_main.xml

     MainActivity.java部分

package com.iddiea.checkbox;

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 eatBox;
    private CheckBox sleepBox;
    private CheckBox playBox;
    
    @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);
        playBox = (CheckBox)findViewById(R.id.playId);
                                        
        OnBoxClickListener listener = new OnBoxClickListener();
        eatBox.setOnClickListener(listener);
        sleepBox.setOnClickListener(listener);
        playBox.setOnClickListener(listener);
}

class OnBoxClickListener implements OnClickListener{

        @Override
        public void onClick(View view) {
            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 if (box.getId()==R.id.playId){
                System.out.println("playBox");
            }
            
            if(box.isChecked()){
            System.out.println("Checkbox is clicked ");
            }
            else {
                System.out.println("Checkbox is clicked ");    
            }
        }

        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
MainActivity.java

 

  2.  OnCheckedChangeListener 的使用方法

    activity_main部分

<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" >

    <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/playId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="玩耍"/>

</LinearLayout>
activity_main

 

     MainActivity.java部分

package com.iddiea.checkbox;

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 eatBox;
    private CheckBox sleepBox;
    private CheckBox playBox;
    
    @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);
        playBox = (CheckBox)findViewById(R.id.playId);
        
        
        CheckBoxListener listener = new  CheckBoxListener();
        eatBox.setOnCheckedChangeListener(listener);
        sleepBox.setOnCheckedChangeListener(listener);
        playBox.setOnCheckedChangeListener(listener);

}
    
    class CheckBoxListener implements OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            if(buttonView.getId()==R.id.eatId){
                System.out.println("eatBox");
            }
            else if (buttonView.getId()==R.id.sleepId){
                System.out.println("sleepBox");
            }
            else if (buttonView.getId()==R.id.playId){
                System.out.println("playBox");        
            }
            
            if(isChecked){
                System.out.println("Checked");
            }
            else {
                System.out.println("UnChecked");
            }
}
        
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
Main_Activity.java

 

  3. 实现 选中All  自动选中其他 子项

    activity_main部分

<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" >

    <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/playId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="玩耍"/>
    
    <CheckBox
        android:id="@+id/allId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="All"/>

</LinearLayout>
activity_main.xml

 

     MainActivity部分

package com.iddiea.checkbox;

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 eatBox;
    private CheckBox sleepBox;
    private CheckBox playBox;
    private CheckBox allBox;
    
    @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);
        playBox = (CheckBox)findViewById(R.id.playId);
        allBox = (CheckBox)findViewById(R.id.allId);
        
        
        CheckBoxListener listener = new  CheckBoxListener();
        eatBox.setOnCheckedChangeListener(listener);
        sleepBox.setOnCheckedChangeListener(listener);
        playBox.setOnCheckedChangeListener(listener);
        allBox.setOnCheckedChangeListener(listener);
        
    }
    
    class CheckBoxListener implements OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

if (buttonView.getId()==R.id.allId) {
                eatBox.setChecked(isChecked);
                sleepBox.setChecked(isChecked);
                playBox.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;
    }

}
MainActivity.java

 

运行结果:

posted @ 2014-03-25 15:16  哎小蝎  阅读(1089)  评论(0编辑  收藏  举报