CheckBox的作用:可以提供复选

下面是我点击按钮查看所选内容的代码:定义按钮监听器,并在onClick方法中调用shoeAlt方法(此方法会在第二块代码定义)

 Button btn=(Button) findViewById(R.id.btn);

         btn.setOnClickListener(new View.OnClickListener() {

             @Override
public void onClick(View v) {
showAlt(); }
});

接下来是定义showAlt方法(首先初始化控件,然后用isCheck方法查看,这些控件哪些被选中,如果被选中就用getText和toString方法得到控件的text)

到这里不得不说Toast

Toast给当前视图显示一个浮动的显示块,它永远不会获得焦点,我会在下一篇的博客中详细解释Toast

 protected CheckBox run, sleep, swim, eat, playball;

     protected void showAlt() {
run = (CheckBox) findViewById(R.id.run);
sleep = (CheckBox) findViewById(R.id.sleep);
swim = (CheckBox) findViewById(R.id.swim);
eat = (CheckBox) findViewById(R.id.eat);
playball = (CheckBox) findViewById(R.id.playball);
StringBuffer str = new StringBuffer();
if (run.isChecked()) {
str.append(run.getText().toString() + ",");
}
if (sleep.isChecked()) {
str.append(sleep.getText().toString() + ",");
}
if (swim.isChecked()) {
str.append(swim.getText().toString() + ",");
}
if (eat.isChecked()) {
str.append(eat.getText().toString() + ",");
}
if (playball.isChecked()) {
str.append(playball.getText().toString() + ",");
}
if (str.length() == 0) {
Toast.makeText(this, "请选择您的爱好", 1).show();
} else {
String str1 = str.substring(0, str.length() - 1);
Toast.makeText(this, "你的爱好是:" + str1, 1).show();
} }

最新文章

  1. ubuntu安装遇到的问题
  2. Mongo技巧-连接数据库与修改表结构
  3. ios开发之ios9UIWebView不显示网页问题
  4. phpcms首页商机的调用,多种方式
  5. 基于nodejs模拟浏览器post请求爬取json数据
  6. 你真的了解volatile吗,关于volatile的那些事
  7. MyBatis框架(三)动态SQL,分页,二进制存入数据库图片
  8. MFC的停靠窗口中插入对话框,在对话框中添加控件并做控件自适应
  9. 《MinDoc 接口文档在线管理系统》
  10. ListView position
  11. pip 18.1: pipenv graph results in ImportError: cannot import name 'get_installed_distributions'
  12. InfoQ 趋势报告:架构和设计领域技术演变详解
  13. STM32.定时器
  14. Visual Studio Code 学习.net core初体验
  15. 搭建markdown图床-腾讯云COS
  16. WebGl 平移(矩阵变换)
  17. PostgreSQL9.6主从配置
  18. MyEclipse for Linux版下载
  19. 在Mac下使用ll,la,l
  20. 转载-lvs官方文档04-LVS集群的负载调度

热门文章

  1. bfc作用
  2. vue动画钩子
  3. sklearn调用SVM算法
  4. python面向对象之练习题1
  5. 2020 NUC 19级第一次训练赛
  6. 第2章 PLC1200 与HMI (TP900) 组合仿真连接 编写1200程序,编写HMI 仿真过程拍摄视频
  7. 第2节 storm路由器项目开发:8 - 9、集群监控软件ganglia的安装和使用
  8. MyISAM/Innodb的区别
  9. Java中Compareable和Comparator两种比较器的区别
  10. C#中的扩展类的理解