Android自带了丰富的基础组件,这次我们要介绍的CheckBox。
先看本次实例代码的运行效果:
首先我们在strings.xml中定义CheckBox和显示的TextView中字符串,代码如下:
<string name="app_name">AndroidWithCheckBox</string>
<string name="hobby">你的爱好是:</string>
<string name="basketball">篮球</string>
<string name="football">足球</string>
而后在main.xml中定义我们的组件,两个CheckBox和一个TextView
<TextView
android:id ="@+id/showText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hobby"
/>
<CheckBox
android:id="@+id/basketballCheckBox"
android:text="@string/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<CheckBox
android:id="@+id/footballCheckBox"
android:text="@string/football"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
在Activity子类中, 先获取到在main.xml中定义的三个组件,而后给CheckBox添加上OnCheckedChangeListener
,具体代码如下:
public class Test extends Activity {
/** Called when the activity is first created. */
private TextView view ;
private CheckBox basketballCheckBox ;
private CheckBox footballCheckBox ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (TextView)findViewById(R.id.showText);
basketballCheckBox = (CheckBox)findViewById(R.id.basketballCheckBox);
footballCheckBox = (CheckBox)findViewById(R.id.footballCheckBox);
basketballCheckBox.setOnCheckedChangeListener(cbListener);
footballCheckBox.setOnCheckedChangeListener(cbListener);
}
private CheckBox.OnCheckedChangeListener cbListener =
new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
String hobby = getString(R.string.hobby);
String basketball = getString(R.string.basketball);
String football = getString(R.string.football);
String showText = "";
if(basketballCheckBox.isChecked()&&footballCheckBox.isChecked()){
view.setText(showText = hobby+basketball+","+football);
}
else if(basketballCheckBox.isChecked()&&!footballCheckBox.isChecked()){
view.setText(showText = hobby+basketball);
}
else if(!basketballCheckBox.isChecked()&&footballCheckBox.isChecked()){
view.setText(showText = hobby+football);
}
else{
view.setText(showText = hobby+"不是下面的两项");
}
Toast.makeText(Test.this, showText, Toast.LENGTH_SHORT).show();
}};
}
- 大小: 18.7 KB
分享到:
相关推荐
NULL 博文链接:https://sarin.iteye.com/blog/1665694
Android学习笔记(三)android studio中CheckBox自定义样式(更换复选框左侧的勾选图像) Android学习笔记(四)Android 中Activity页面的跳转及传值 Android学习笔记(五)——Toast提示、Dialog对话框、Menu菜单 ...
Android学习笔记(三)android studio中CheckBox自定义样式(更换复选框左侧的勾选图像) Android学习笔记(四)Android 中Activity页面的跳转及传值 Android学习笔记(五)——Toast提示、Dialog对话框、Menu菜单 ...
对于完成的CheckBox就直接放在布局中就可,但对于删除不想使用ContextMenu来实现编辑,对于像iOS中那样的列表,它的删除都是通过对列表中每个项目的手势来触发。这个实现起来并不难,可以用一个ViewSwitcher,...
Android学习笔记(三)android studio中CheckBox自定义样式(更换复选框左侧的勾选图像) Android学习笔记(四)Android 中Activity页面的跳转及传值 Android学习笔记(五)——Toast提示、Dialog对话框、Menu菜单 ...
Android学习笔记(三)android studio中CheckBox自定义样式(更换复选框左侧的勾选图像) Android学习笔记(四)Android 中Activity页面的跳转及传值 Android学习笔记(五)——Toast提示、Dialog对话框、Menu菜单 ...
Android3D游戏开发付费视频教程共享(更新第四集) 史上最全示例Android教学视频,非常值得观看 Android游戏开发系列源码+CHM+书籍截图+目录】 Android developer guide中文翻译文档 Android开发开发技巧之 EditText...
对一些控件的解释,和使用,和监听以及一些页面跳转,和数据库,和Dialog 和布局的使用listView的使用,下拉菜单,Menu,元素的属性,CheckBox,适配器(Adapter),XML解析,android 中的画图,等。
如果没有特殊要求,我们可以使用Android提供的框架来创建系统样式的Preference Screen,在其内部可以包含PreferenceCategory和Preference元素的任意组合。建立Preference Screen,需要向里面添加首选项控件,原生的...
《Android应用开发I》实验指导书 计算机科学及技术系 目 录 实验一 :第一个应用程序 3 实验二 :基本控件 1 实验三 :高级控件 4 实验四 :菜单和对话框 7 实验五 :数据存储 8 实验六 :后台处理及获取网络数据 9 ...
Android学习笔记2-6~2-8 推荐新手向学习视频:B站https://www.bilibili.com/video/av38409964点我传送 2-6 复选框CheckBox 常用属性 自定义样式 bg_checkbox.xml文件 未选中时 选中时 监听事件 ...
整理一份自己学习android的合集(现在整理下以前的笔记,写作一个demo作为总结,也是查漏补缺,同时可以比较下以前与现在自己的能力提升了多少!) 从2018/1/23开始 新建项目,计划——开始整理; 2018/1/26, 常用...
文章目录Android 基本控件文本框 TextView基本属性其他属性编辑框 EditText普通按钮 Button图片按钮ImageButton单选按钮 RadioButton复选按钮 CheckBox图像视图 ImageView使用私有对象响应按钮点击题目笔记 ...
雾山的Anrdoid学习笔记---CheckBox,RadioGroup&RadioButton的配套资源http://blog.csdn.net/tt75281920/article/details/26248451