Android 开发之CheckBox

本文采用SDK版本 Android 4.2.2

CheckBox就是我们平常上网看到的复选框,即多选控件,比如:我们注册用户的时候经常可以看到,也是Android中经常用到的。

关于CheckBox有2个重要事件;

  •  OnClickListener            CheckBox点击是触发
  • OnCheckedChangeListener   改变CheckBox选择状态是触发

首先,我们来看下第一个事件,具体代码如下:

布局文件:activity_main.xml

<RelativeLayout 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: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/cbox1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="玩游戏"
         />
    <CheckBox
        android:id="@+id/cbox2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/cbox1"
        android:text="睡觉"
         />
    <CheckBox
        android:id="@+id/cbox3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/cbox2"
        android:text="吃饭"
         />

    
</RelativeLayout>

 java文件

package com.zhoucj.messagedemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	CheckBox cbox1;
	CheckBox cbox2;
	CheckBox cbox3;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		cbox1=(CheckBox)findViewById(R.id.cbox1);
		cbox2=(CheckBox)findViewById(R.id.cbox2);
		cbox3=(CheckBox)findViewById(R.id.cbox3);
		
		cbox1.setOnClickListener(new cbox());
		cbox2.setOnClickListener(new cbox());
		cbox3.setOnClickListener(new cbox());
	}
	
	class cbox implements OnClickListener
	{
		@Override
		public void onClick(View v) {
			if(v.getId()==R.id.cbox1)
			{
				showToast("您选择了玩游戏");
			}else if(v.getId()==R.id.cbox2)
			{
				showToast("您选择了睡觉");
			}else if(v.getId()==R.id.cbox3)
			{
				showToast("您选择了吃饭");
			}
			
		}
	}
	
	public void showToast(String msg)
	{
		Toast.makeText(this, msg, 2000).show();
	}
	
	@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;
	}

}

 运行结果:

可以看出,OnClickListener    不管你选没选中,都会触发。也就是说,只要当用户点击了,就触发了该事情。

下面我们来看看OnCheckedChangeListener  这个事情使用方法。

布局文件和上面的是一样的,这里只把java代码贴出来。

package com.zhoucj.messagedemo;

import android.app.Activity;
import android.os.Bundle;
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;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	CheckBox cbox1;
	CheckBox cbox2;
	CheckBox cbox3;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		cbox1=(CheckBox)findViewById(R.id.cbox1);
		cbox2=(CheckBox)findViewById(R.id.cbox2);
		cbox3=(CheckBox)findViewById(R.id.cbox3);
		
		cbox1.setOnCheckedChangeListener(new cbox());
		cbox2.setOnCheckedChangeListener(new cbox());
		cbox3.setOnCheckedChangeListener(new cbox());
	}
	
	class cbox implements OnCheckedChangeListener
	{
		//buttonView:被选中 那个CheckBox对象,既你选择了那个CheckBox返回那个该控件的对象
		//isChecked:返回该CheckBox是否选中
		@Override
		public void onCheckedChanged(CompoundButton buttonView,	boolean isChecked) {
			if(isChecked) //isChecked 只有当CheckBox选中是才执行
			{
				CheckBox ck=(CheckBox)buttonView;
				showToast(ck.getText().toString());
			}
		}
	}
	
	public void showToast(String msg)
	{
		Toast.makeText(this, msg, 2000).show();
	}
	@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;
	}

}

运行结果如下:

CheckBox是常见的控件也是常用的控件之一,掌握是有必要的。

好了,今天就说到这里了。

作者:zhoucj

原创作品

原文地址:https://www.cnblogs.com/zhoujian315/p/3131447.html