安卓入门.多选按钮CheckBox

多选按钮CheckBox


1.在XML文件中,使用<CheckBox/>标签
<CheckBox 
        android:id="@+id/eat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="吃饭"
        />
    <CheckBox 
        android:id="@+id/sleep"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="睡觉"
        />
    <CheckBox 
        android:id="@+id/dota"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dota"
        />


2.在代码中,使用CheckBox的对象来表示
private CheckBox eatBox;
	private CheckBox sleepBox;
	private CheckBox dotaBox;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		eatBox=(CheckBox)findViewById(R.id.eat);
		sleepBox=(CheckBox)findViewById(R.id.sleep);
		dotaBox=(CheckBox)findViewById(R.id.dota);
	}


3.常用监听器OnClickListener接口和OnCheckedChangeListener 接口
用类来实现这些接口



//OnClickListener
class CheckBoxClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {//view是CheckBox的父类,所以CheckBox可以向上转型
			if (v.getId() == R.id.eat) {
				System.out.println("eat");
			} else if (v.getId() == R.id.sleep) {
				System.out.println("sleep");
			} else if (v.getId() == R.id.dota) {
				System.out.println("dota");
			}
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		eatBox = (CheckBox) findViewById(R.id.eat);
		sleepBox = (CheckBox) findViewById(R.id.sleep);
		dotaBox = (CheckBox) findViewById(R.id.dota);

		CheckBoxClickListener checkBoxClickListener = new CheckBoxClickListener();
		eatBox.setOnClickListener(checkBoxClickListener);
		sleepBox.setOnClickListener(checkBoxClickListener);//监听器绑定控件
		dotaBox.setOnClickListener(checkBoxClickListener);
	}	

================


//实现OnCheckedChangeListener接口
	// 单选框,isChecked(被选中)或者UnCheck
	class CheckBoxChange implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (buttonView.getId() == R.id.dota) {
				System.out.println("dotaBox");
			} else if (buttonView.getId() == R.id.eat) {
				System.out.println("eatBox");
			} else if (buttonView.getId() == R.id.sleep) {
				System.out.println("sleepBox");
			}

			if (isChecked) {
				System.out.println("checked");
			} else {
				System.out.println("Unchecked");
			}
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		eatBox = (CheckBox) findViewById(R.id.eat);
		sleepBox = (CheckBox) findViewById(R.id.sleep);
		dotaBox = (CheckBox) findViewById(R.id.dota);

		// CheckBoxClickListener checkBoxClickListener = new
		// CheckBoxClickListener();
		// eatBox.setOnClickListener(checkBoxClickListener);//监听器绑定控件
		// sleepBox.setOnClickListener(checkBoxClickListener);//监听器绑定控件
		// dotaBox.setOnClickListener(checkBoxClickListener);//监听器绑定控件

		CheckBoxChange checkBoxChange = new CheckBoxChange();

		eatBox.setOnCheckedChangeListener(checkBoxChange);//监听器绑定控件
		sleepBox.setOnCheckedChangeListener(checkBoxChange);//监听器绑定控件
		dotaBox.setOnCheckedChangeListener(checkBoxChange);//监听器绑定控件
	}

相关推荐