Android控件开发之CheckBox

CheckBox,也就是多项选择。Android中提供了ChechBox控件,使用起来非常方便。


CheckBox效果



main.xml源码



  
     
   
    
   
    
  
 
  
     
  
     


本程序java源码

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

public class CheckBoxActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
                
	final CheckBox check1 = (CheckBox)findViewById(R.id.check1);
	final CheckBox check2 = (CheckBox)findViewById(R.id.check2);
	final CheckBox check3 = (CheckBox)findViewById(R.id.check3);
	final CheckBox check4 = (CheckBox)findViewById(R.id.check4);
	final CheckBox check5 = (CheckBox)findViewById(R.id.check5);
        
      	//创建CheckBox事件监听器
          check1.setOnCheckedChangeListener(listener);
         check2.setOnCheckedChangeListener(listener);
         check3.setOnCheckedChangeListener(listener);
         check4.setOnCheckedChangeListener(listener);
         check5.setOnCheckedChangeListener(listener);
        
    }
     
    private OnCheckedChangeListener listener = new OnCheckedChangeListener()
    {
	@Override
	public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
	{
		switch(buttonView.getId())
		{
		case R.id.check1:
			if(isChecked)
				Toast.makeText(getApplicationContext(), "你喜欢苹果 ios智能手机系统", Toast.LENGTH_LONG).show();
			break;
		case R.id.check2:
			if(isChecked)
				Toast.makeText(getApplicationContext(), "你喜欢谷歌 Android智能手机系统", Toast.LENGTH_LONG).show();
			break;
		case R.id.check3:
			if(isChecked)
				Toast.makeText(getApplicationContext(), "你喜欢RIM BlackBerry智能手机系统",Toast.LENGTH_LONG).show();
			break;
		case R.id.check4:
			if(isChecked)
				Toast.makeText(getApplicationContext(), "你喜欢微软 Windows phone 7智能手机系统", Toast.LENGTH_LONG).show();
			break;
		case R.id.check5:
			if(isChecked)
				Toast.makeText(getApplicationContext(), "你喜欢诺基亚 symbian智能手机系统", Toast.LENGTH_LONG).show();
			break;
			}
		}		     
    };

你可能感兴趣的:(Android,控件开发)