Android开发之RadioButton和CheckBox
来源:互联网 发布:顾客关系管理数据 编辑:程序博客网 时间:2024/06/06 08:32
如下图像是整体的布局
这是main.xml
<?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" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><RadioGroupandroid:id="@+id/gender"android:orientation="horizontal"android:layout_width="fill_parent" android:layout_height="wrap_content" > <RadioButton android:id="@+id/man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/man" /> <RadioButton android:id="@+id/woman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/woman" /></RadioGroup><CheckBoxandroid:id="@+id/run"android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/run"/><CheckBoxandroid:id="@+id/sing"android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/sing"/><CheckBoxandroid:id="@+id/dance"android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/dance"/></LinearLayout>
这是RadioButton触发事件的核心代码
gender = (RadioGroup)findViewById(R.id.gender); man = (RadioButton)findViewById(R.id.man); woman = (RadioButton)findViewById(R.id.woman); gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stub if(man.getId()==checkedId){ //System.out.println("男"); Toast.makeText(radioTest.this, "我是帅哥", Toast.LENGTH_LONG).show(); }else if(woman.getId()==checkedId){ //System.out.println("女"); Toast.makeText(radioTest.this, "我是美女", Toast.LENGTH_LONG).show(); }}});
这是CheckBox的触发事件的核心代码
run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){//System.out.println("run checked");Toast.makeText(radioTest.this, "感觉很好!", Toast.LENGTH_LONG).show();}else{//System.out.println("run unchecked");Toast.makeText(radioTest.this, "坑爹啊!", Toast.LENGTH_LONG).show();}}});
从中我们可以看到单选按钮触发事件是绑定到了RadioGroup上面,而多选按钮是绑定到了每一个多选按钮上。此外,我们看到还有Toast,这是一个提示消息,就像上面图像里的一样,用法很简单,就是一句话
Toast.makeText(radioTest.this, "我是美女", Toast.LENGTH_LONG).show();
我们依葫芦画瓢就可以了
- Android开发之RadioButton和CheckBox
- android开发的CheckBox和RadioButton
- Android控件之Checkbox和RadioButton
- Android CheckBox和RadioButton
- android控件之radiobutton checkbox
- Android控件之CheckBox、RadioButton
- Android开发--控件之RadioGroup、RadioButton、CheckBox、Toast
- mars android开发之六:radioButton与checkbox
- Android-CheckBox和RadioButton复习
- Android控件CheckBox和RadioButton
- Android RadioButton和CheckBox类
- Android开发学习笔记:RadioButton和CheckBox浅析
- Android开发--RadioButton和CheckBox控件的使用
- Android开发学习笔记:RadioButton和CheckBox浅析
- Android开发学习笔记——浅谈RadioButton和CheckBox
- Android常见控件之RadioGroup,RadioButton,CheckBox和Toast
- 10、从头学Android之RadioButton和CheckBox
- Android UI学习笔记 之RadioButton和CheckBox的使用
- Spring AOP 详解
- 移动互联网,是团购网站的救命稻草吗?
- open函数学习
- jquery设置弹窗效果
- 成功的企业家懂得放弃和专注
- Android开发之RadioButton和CheckBox
- 多核平台下的JAVA优化
- Jetty安装部署
- 90後总有一些人:关于90后的那些点点滴滴
- 静态库和动态库的区别
- struts2 文件下載 問題
- 多核系统上的 Java 并发缺陷模式(bug patterns)
- Android OTA 升级
- Sicily 1295. 负权数