CheckBox介绍与应用
来源:互联网 发布:三网合一建站系统源码 编辑:程序博客网 时间:2024/05/31 18:49
CheckBox一般用来提供给用户输入信息的组件,可以一次选择多个选项.这个组件解决了不是很方便的手机屏幕操作输入时.用选择组件供用户单击输入选项,显得非常有用.
在Android中,和其他组件一样,也是在xml中定义,安装前面讲解的其他组件的编程思路,
首先在main.xml文件中再增加一个按钮"CheckBox",单击该按钮程序运行将展示介绍CheckBox的界面.
<Button android:id = "@+id/check_box" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Check Box"/>代码解释:
定义一个id为check_box的Button ,并设定其宽度和高度都是自适应,并且指定其显示的文字是"Check Box"
其对应的响应方法代码如下:
Button bt = (Button)findViewById(R.id.check_box);bt.setOnClickListener(new OnClickListener(){ public void OnClick(View v) { Intent intent = new Intent(mainActivity.this,CheckBoxActivity.class); startActivity(intent); }});代码解释:
在单击按钮的时候,启动CheckBoxActivity,这个CheckBoxActivity就是用来展示CheckBox组件的.
修改CheckBoxActivity的onCreate方法,实现代码如下:
public void onCreate(savedInstanceState){ super.onCreate(savedInstanceStete); setTitle("CheckBoxActivity"); setContentView(R.layout.check_box);}代码解释:
上述代码是将其关联在check_box.xml的布局模板上,
然后需要在里面添加CheckBox组件,代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation = "vertical"android:layout_width = "fill_parent"android:layout_height = "fill_parent"><CheckBox android:id = "@+id/plain_cb"android:text = "Plain"android:layout_width = "wrap_content"android:layout_height = "wrap_content"/><CheckBox android:id = @+id/serif_cb"andaroid:text = "Serif"android:layout_width = "wrap_content"android:layout_height = "wrap_content"android:typeface = "serif"/><CheckBox android:id = "@+id/bold_cb"android:text = "Bold"android:layout_width ="wrap_content"android:layout_height = "wrap_content"android:textStyle = "bold"/><CheckBox android:id = "@+id/italic_cb"android:text = "Italic"android:layout_width = "wrap_content"android:layout_height = "wrap_content"android:textStyle = "italic"/></LinearLayout>代码解释:
上述代码中实现添加4个CheckBox组件,并设定各自的宽度和高度都是自适应的,
然后需要考虑一下如何处理CheckBox的选中或者未选中,在这个应用中添加一个Button按钮,单击Button按钮时,程序运行获取4个Checkbox的状态,进而将被选中的Checkbox的值显示在Title 上.
首先在check_box.xml文件中添加一个Button按钮,实现代码如下:
<Button android:id = "@+id/button"android:layout_width = "wrap_content"android:layout_height = "wrap_content"android:text = "显示Check Box的值"/>然后编写其对应的单击响应方法,实现代码如下所示:
Button bt = (Button) findViewById(R.id.button);bt.setOnClickListener(new OnClickListener(){ OnClick(View v) { StringBuffer sb = ""; if(plain_cb.isChecked()) { sb = plain_cb.getText(); } if(serif_cb.isChecked()) { sb = sb + " , " + serif_cb.getText(); } if(italic_cb.isChecked()) { sb = sb + " , " + italic_cb.getText(); } if(bold_cb.isChecked()) { sb = sb + " , " + bold_cb.getText(); } setTitle("Checked: "+ sb); }});
0 0
- CheckBox介绍与应用
- checkbox 与JS的应用
- checkbox应用
- checkbox应用
- HTML介绍与应用
- 遥感技术与应用介绍
- Grunt介绍与应用
- Maven介绍与应用
- javaScript介绍与应用
- NVDIMM介绍与应用
- monkeyruuner介绍与应用
- Android之RadioButton与RadioGroup,CheckBox,Switch,ToggleButton的介绍
- LoadRunner介绍与应用实例
- 传智播客 Lucene介绍与应用
- RPM的介绍与应用
- PHP PDO介绍与应用
- LoadRunner介绍与应用实例
- redis事物介绍与应用
- 程序中有一条语句,想让它5秒钟之后
- SDL_Init()代码阅读
- OSGi——运行工具箱的配置(org.eclipse.equinox.http org.eclipse.equinox.servlet.api)问题
- 【学习笔记】将xml node 格式化输出为string
- Learn Objective‐C
- CheckBox介绍与应用
- 浅谈----指针和引用的区别
- [Android蓝牙]搜索周围蓝牙设备
- Domino服务器的POP3任务挂起
- 中山市菲诺浦电子厂 创业赢利赚钱的两大妙招!
- Handler Class Should be Static or Leaks Occur
- 开视频聊天室怎么赚钱 如何利用聊天室赚钱
- Introducing practical and robust anomaly detection in a time series
- Linux——CentOS下安装MPlayer