//有幾個CheckBox 控件 和一個Button控件
一、res/layout的佈局 界面
代碼android
<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="wrap_content"
android:orientation="vertical"
>app
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="吃飯"
android:textSize="50sp"/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="睡覺"
android:textSize="50sp"/>
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="打豆豆"
android:textSize="50sp"/>
<CheckBox
android:id="@+id/checkall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="全選/反選"
android:textSize="50sp"
android:onClick="onclick"/>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="50sp"
android:onClick="onclick"/>
ide
</LinearLayout>佈局
-----------------------------this
二、在MainActivity 裏 實現功能xml
代碼對象
public class MainActivity extends Activity {
//聲明 控件 的對象
private CheckBox checkbox1;
private CheckBox checkbox2;
private CheckBox checkbox3;
private CheckBox checkall;get
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取控件 的id
checkbox1 = (CheckBox) this.findViewById(R.id.checkbox1);
checkbox2 = (CheckBox) this.findViewById(R.id.checkbox2);
checkbox3 = (CheckBox) this.findViewById(R.id.checkbox3);
checkall = (CheckBox) this.findViewById(R.id.checkall);
checkbox1.setOnCheckedChangeListener(oncheckchange);
checkbox2.setOnCheckedChangeListener(oncheckchange);
checkbox3.setOnCheckedChangeListener(oncheckchange);
checkall.setOnCheckedChangeListener(oncheckchange);
}
//用OnCheckedChangeListener 的監聽 方法
public OnCheckedChangeListener oncheckchange = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonview, boolean ischeck) {
// TODO Auto-generated method stub
if(buttonview.isChecked()){
Toast.makeText(MainActivity.this, "你選擇了:" + getResult(), Toast.LENGTH_SHORT).show();
}
}
};
public String getResult(){
StringBuffer sb = new StringBuffer();
if(this.checkbox1.isChecked()){
sb.append(this.checkbox1.getText());
}
if(this.checkbox2.isChecked()){
sb.append(this.checkbox2.getText());
}
if(this.checkbox3.isChecked()){
sb.append(this.checkbox3.getText());
}
return sb.toString();
}
public void onclick(View view){
switch(view.getId()){
case R.id.checkall:
//把checkall 的狀態(選中或沒選) 賦值給 其餘 checkbox
boolean checkvalue = checkall.isChecked();
checkbox1.setChecked(checkvalue);
checkbox2.setChecked(checkvalue);
checkbox3.setChecked(checkvalue);
Toast.makeText(MainActivity.this, "你選擇了:" + getResult(), Toast.LENGTH_SHORT).show();
break;
case R.id.submit:
Toast.makeText(MainActivity.this, "你選擇了:" + getResult(), Toast.LENGTH_SHORT).show();
break;
default:
}
}
}it