Android控件之CheckBox、RadioButton
2015-11-19 14:42
387 查看
1、复选框CheckBox控件
public class CheckBox extend CompoundButton
复选框允许用户选择一个或多个选项。通常情况下,是从一个垂直列表中选择多个选项。每个复选框是分开管理的,必须为每个复选框注册监听事件。注册click监听事件的方式有两种。
xml布局文件如下:
第一种:在使用该xml布局文件的Activity类实现CompoundButton.OnCheckedChangeListener接口
第二种:使用内部类的方式,对每一个复选框注入单击事件
2、单选框RadioButton控件
单选按钮允许用户从一组中选择一个选项。由于单选按钮是相互排斥的,通过RadioGroup将多个RadioButton组合到一起,这样可以确保只能选择一个选项。
事件处理:onRadioButtonClicked。
public class CheckBox extend CompoundButton
复选框允许用户选择一个或多个选项。通常情况下,是从一个垂直列表中选择多个选项。每个复选框是分开管理的,必须为每个复选框注册监听事件。注册click监听事件的方式有两种。
xml布局文件如下:
<CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginLeft="23dp" android:text="看小说" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/checkBox1" android:layout_below="@+id/checkBox1" android:text="打网游" /> <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/checkBox2" android:layout_below="@+id/checkBox2" android:text="看电影" />
第一种:在使用该xml布局文件的Activity类实现CompoundButton.OnCheckedChangeListener接口
package com.example.checkboxradiobutton; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.Toast; public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener { private CheckBox cb1,cb2,cb3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cb1 = (CheckBox) this.findViewById(R.id.checkBox1); cb2 = (CheckBox) this.findViewById(R.id.checkBox2); cb3 = (CheckBox) this.findViewById(R.id.checkBox3); cb1.setOnCheckedChangeListener(this); cb2.setOnCheckedChangeListener(this); cb3.setOnCheckedChangeListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } /** * @param compoundButton 被单击的复选框 * @param isChecked 复选框选中为true,没有选中为false */ @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { switch (compoundButton.getId()) { case R.id.checkBox1: if (isChecked) Toast.makeText(this, "选中看小说", Toast.LENGTH_SHORT).show(); else Toast.makeText(this, "取消看小说", Toast.LENGTH_SHORT).show(); break; case R.id.checkBox2: if (isChecked) Toast.makeText(this, "选中打网游", Toast.LENGTH_SHORT).show(); else Toast.makeText(this, "取消打网游", Toast.LENGTH_SHORT).show(); break; case R.id.checkBox3: if (isChecked) Toast.makeText(this, "选中看电影", Toast.LENGTH_SHORT).show(); else Toast.makeText(this, "取消看电影", Toast.LENGTH_SHORT).show(); break; } } }
第二种:使用内部类的方式,对每一个复选框注入单击事件
cb1 = (CheckBox) this.findViewById(R.id.checkBox1); cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { } });
2、单选框RadioButton控件
单选按钮允许用户从一组中选择一个选项。由于单选按钮是相互排斥的,通过RadioGroup将多个RadioButton组合到一起,这样可以确保只能选择一个选项。
<RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/checkBox3" android:layout_below="@+id/textView2" > <RadioButton android:id="@+id/radio0" android:layout_width="92dp" android:layout_height="match_parent" android:checked="true" android:text="男" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup>
事件处理:onRadioButtonClicked。
相关文章推荐
- android定制 Launcher
- android 5.0 新特性
- Android音频系统之音频框架
- android四大组件之Service 播放音乐
- 【Android】长按连续触发事件的实现方法
- Android编程实现仿心跳动画效果的方法
- android录制声音写入文件
- 关于android应用的toolbar(actionbar)不能填满布局解决
- Android在SDCard创建目录_卸载应用同时删除工程创建的文件
- Android 4.4前后版本读取图库图片和拍照完美解决方案
- android自定义状态栏的颜色
- Android 工程师如何快速学会web前段
- button设置点击更改背景图片
- 如何创建上下文菜单
- Android 设置Activity窗体 不显示标题和全屏显示
- Android Animation实现元素在屏幕上按照指定轨迹运动,以及出现NullPointerException的解决方案
- Android编程之界面跳动提示动画效果实现方法
- Android 工程师如何快速学会web前段
- Android学习碎片(二)——如何从本地图片获得Bitmap实例
- 【异常】Configure Android Studio such that source detected in git but Unregistered Vcs root detected