星空网 > 软件开发 > 操作系统

多选按钮(CheckBox)

今天我们介绍的是Checkbox多选框:

多选按钮(CheckBox)images/loading.gif' data-original="http://images2015.cnblogs.com/blog/796122/201604/796122-20160409174824343-994587552.png" width="401" height="122" />

1.Activity

//复选框,[基础控件]---状态切换控件CompoundButton及其子类CheckBox、RadioButton、ToggleButton、switch事件监听与场景使用public class CheckBoxActivity extends Activity implements CompoundButton.OnCheckedChangeListener{    private Context context;  private CheckBox sleepCheckBox;  private CheckBox dadoudouCheckBox;  private CheckBox gameCheckBox;  private CheckBox shoppingCheckBox;  private CheckBox filmCheckBox;  private CheckBox sportCheckBox;  private Button submitButton;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.check_box);        init();    addAction();      }    private void init(){    context = this;    sleepCheckBox = (CheckBox)findViewById(R.id.sleepCheckBoxId);    dadoudouCheckBox = (CheckBox)findViewById(R.id.dadoudouCheckBoxId);    gameCheckBox = (CheckBox)findViewById(R.id.gameCheckBoxId);    shoppingCheckBox = (CheckBox)findViewById(R.id.shoppingCheckBoxId);    filmCheckBox = (CheckBox)findViewById(R.id.filmCheckBoxId);    sportCheckBox = (CheckBox)findViewById(R.id.sportCheckBoxId);    submitButton = (Button)findViewById(R.id.submitButtonId);  }    private void addAction(){    sleepCheckBox.setOnCheckedChangeListener(this);    dadoudouCheckBox.setOnCheckedChangeListener(this);    gameCheckBox.setOnCheckedChangeListener(this);    shoppingCheckBox.setOnCheckedChangeListener(this);    filmCheckBox.setOnCheckedChangeListener(this);    sportCheckBox.setOnCheckedChangeListener(this);    submitButton.setOnClickListener(new View.OnClickListener() {      public void onClick(View v) {        //String 字符串常量        //StringBuffer 字符串变量(线程安全)        //StringBuilder 字符串变量(非线程安全)        StringBuilder sb = new StringBuilder("您的兴趣是:");        //MyStringBuilder.Insert(6,"Beautiful ");        //MyStringBuilder.Remove(5,7);        //MyStringBuilder.Replace('!', '?');        //代码示例指定可以将 MyStringBuilder对象扩充到最大 25个空白。        //StringBuilderMyStringBuilder = new StringBuilder("Hello World!", 25);        if(sleepCheckBox.isChecked()){          sb.append("睡觉 ");        }        if(dadoudouCheckBox.isChecked()){          sb.append("打豆豆 ");        }        if(gameCheckBox.isChecked()){          sb.append("游戏 ");        }        if(shoppingCheckBox.isChecked()){          sb.append("购物 ");        }        if(filmCheckBox.isChecked()){          sb.append("电影 ");        }        if(sportCheckBox.isChecked()){          sb.append("运动");        }                Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();      }    });  }  @Override  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {    int id = buttonView.getId();    switch(id){    case R.id.sleepCheckBoxId:      if(isChecked){        Toast.makeText(context, "你选择了\"睡觉\"", Toast.LENGTH_SHORT).show();      }else{        Toast.makeText(context, "你取消选择了\"睡觉\"", Toast.LENGTH_SHORT).show();      }      break;    case R.id.dadoudouCheckBoxId:      if(isChecked){        Toast.makeText(context, "你选择了\"打豆豆\"", Toast.LENGTH_SHORT).show();      }else{        Toast.makeText(context, "你取消选择了\"打豆豆\"", Toast.LENGTH_SHORT).show();      }      break;    case R.id.gameCheckBoxId:      if(isChecked){        Toast.makeText(context, "你选择了\"游戏\"", Toast.LENGTH_SHORT).show();      }else{        Toast.makeText(context, "你取消选择了\"游戏\"", Toast.LENGTH_SHORT).show();      }      break;    case R.id.shoppingCheckBoxId:      if(isChecked){        Toast.makeText(context, "你选择了\"购物\"", Toast.LENGTH_SHORT).show();      }else{        Toast.makeText(context, "你取消选择了\"购物\"", Toast.LENGTH_SHORT).show();      }      break;    case R.id.filmCheckBoxId:      if(isChecked){        Toast.makeText(context, "你选择了\"电影\"", Toast.LENGTH_SHORT).show();      }else{        Toast.makeText(context, "你取消选择了\"电影\"", Toast.LENGTH_SHORT).show();      }      break;    case R.id.sportCheckBoxId:      if(isChecked){        Toast.makeText(context, "你选择了\"运动\"", Toast.LENGTH_SHORT).show();      }else{        Toast.makeText(context, "你取消选择了\"运动\"", Toast.LENGTH_SHORT).show();      }      break;    }  }  }

2.

<?  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  android:padding="5dp" >  <TextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="兴趣:"    android:textSize="20sp" />  <LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal" >    <CheckBox      android:id="@+id/sleepCheckBoxId"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="睡觉" />    <CheckBox      android:id="@+id/dadoudouCheckBoxId"      android:layout_width="85dp"      android:layout_height="wrap_content"      android:text="打豆豆" />    <CheckBox      android:id="@+id/gameCheckBoxId"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="游戏" />  </LinearLayout>  <LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal" >    <CheckBox      android:id="@+id/shoppingCheckBoxId"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="购物" />    <CheckBox      android:id="@+id/filmCheckBoxId"      android:layout_width="85dp"      android:layout_height="wrap_content"      android:text="电影" />    <CheckBox      android:id="@+id/sportCheckBoxId"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="运动" />  </LinearLayout>  <Button    android:id="@+id/submitButtonId"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="right"    android:text="确定"    android:textSize="20sp" /></LinearLayout>

3.效果图如下:

多选按钮(CheckBox)




原标题:多选按钮(CheckBox)

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

欧代信息标签:https://www.goluckyvip.com/tag/73059.html
黑五没爆单:https://www.goluckyvip.com/tag/7306.html
欧代作用:https://www.goluckyvip.com/tag/73060.html
欧代的作用:https://www.goluckyvip.com/tag/73061.html
英代 欧代:https://www.goluckyvip.com/tag/73062.html
欧代标志:https://www.goluckyvip.com/tag/73063.html
海陵岛马尾岛景点介绍 海陵马尾岛图片:https://www.vstour.cn/a/363177.html
无锡旅游景点竹海 - 无锡的竹海:https://www.vstour.cn/a/363178.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流