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