CheckBox 控件的使用 day2.1

本文详细介绍了如何在Android应用中创建并使用自定义多选按钮,包括布局设计、控件交互及功能实现,并通过实例展示了多选按钮在实际应用中的运用。

//有几个CheckBox 控件 和一个Button控件
1、res/layout的布局 界面
代码

<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"
    >

    <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"/>
   

</LinearLayout>

-----------------------------

2、在MainActivity 里 实现功能

代码

public class MainActivity extends Activity {
//声明 控件 的对象
private CheckBox checkbox1;
private CheckBox checkbox2;
private CheckBox checkbox3;
private CheckBox checkall;

 @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:
 }
}
}

转载于:https://my.oschina.net/u/2542711/blog/600173

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值