创建布局文件*声明一组多选按钮,设置各个按钮id .*
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
***创建一组多选按钮,设置各个按钮id ,统一由allcheck(全选按钮控制)***
<CheckBox
android:id="@+id/eat"
android:text="吃饭"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
/>
<CheckBox
android:id="@+id/sleep"
android:text="睡觉"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<CheckBox
android:id="@+id/dota"
android:text="打dota"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>**重点内容**
<CheckBox
android:id="@+id/allChenked"
android:text="全选/全不选"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
</LinearLayout>
MainActivity 中使用findViewById 获取对象,再为CheckBox 绑定相应的监听器(OnCheckedListener 以及OnCheckedChangeListener(在按钮选中状态发生改变时被调用))
public class MainActivity extends ActionBarActivity {
private CheckBox eatBox;
private CheckBox sleepBox;
private CheckBox dotaBox;
private CheckBox allCheckedBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
**//此处通过findViewById 找到相应的CheckBox**
------------------------------------
eatBox=(CheckBox)findViewById(R.id.eat);
sleepBox=(CheckBox)findViewById(R.id.sleep);
dotaBox=(CheckBox)findViewById(R.id.dota);
allCheckedBox=(CheckBox)findViewById(R.id.allChenked);
//生成监听器对象,给AllCheckBoxListener 实现监听器接口
AllCheckBoxListener listener = new AllCheckBoxListener();
allCheckedBox.setOnCheckedChangeListener(listener);
//定义AllCheckBoxListener 类,用来实现监听器接口。
class AllCheckBoxListener implements CompoundButton.OnCheckedChangeListener{
// CompoundButton.OnCheckedChangeListener 用来监听CheckBox 对象的状态变化
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//该方法内实现当allCheckedBox 状态变化时,eatBox,sleepBox,dataBox 三个CheckBox状态随之变化
eatBox.setChecked(b);
sleepBox.setChecked(b);
dotaBox.setChecked(b);
//或者更改为复杂的代码方式
// if (allCheckedBox=isChecked()){
// eatbox.setChecked(ture);
// sleepbox.setChecked(ture);
// dotabox.setChecked(ture);
// }else{
// eatbox.setChecked(false);
// sleepbox.setChecked(false);
// dotabox.setChecked(false);
// }
//
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}