CheckBox 控件的使用 day2.1

//有几个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

相关文章
相关标签/搜索