RadioButton、CheckBox与ToggleButton
1.RadioButton
RadioButton被称作为单选框,通常都是以组的形式出现,可以在一组控件中选择一个。
RadioButton的使用首先需要加入<RadioGroup/>,在这个组中,我们进行单选按钮的声明。
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="51dp"
android:layout_y="182dp" > <RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="172dp"
android:layout_y="181dp"
android:text="关灯" /> <RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="36dp"
android:layout_y="201dp"
android:text="开灯" />
</RadioGroup>
RadioButton
这里我们定义了两个RadioButton按钮,用来控制图片的切换,我们需要为RadioButton添加监听事件
Button myButton;
ImageButton myImg;
TextView textView;
ToggleButton myToggle;
ImageView img;
CheckBox myCheck; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton=(Button)findViewById(R.id.button1);
textView=(TextView)findViewById(R.id.text1);
myToggle=(ToggleButton)findViewById(R.id.toggleButton1);
myCheck=(CheckBox)findViewById(R.id.checkBox1);
RadioButton radio=(RadioButton)findViewById(R.id.radioButton2);
RadioButton radio1=(RadioButton)findViewById(R.id.radioButton1);
radio1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO 自动生成的方法存根
setBulbState(isChecked);
}
});
radio.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO 自动生成的方法存根
setBulbState(isChecked);
}
}); }
private void setBulbState(boolean isChecked) {
// TODO 自动生成的方法存根
img=(ImageView)findViewById(R.id.imageView1); img.setImageResource((isChecked)?R.drawable.bulbon:R.drawable.buldoff); RadioButton radio1=(RadioButton)findViewById(R.id.radioButton1);
radio1=(RadioButton)findViewById(R.id.radioButton1);
radio1.setChecked(isChecked);
radio1=(RadioButton)findViewById(R.id.radioButton2);
radio1.setChecked(!isChecked); }
RadioButton监听
这里我们通过findViewById()来获取控件,并实现了控件的监听 setonCheckedChangeListener;
2.CheckBox
CheckBox控件被称为复选框,我们通过判断控件的选中状态,控制图片的切换。在资源文件中添加两个String对象,分别对应checkbox的选中状态,checkbox可以在不同的状态显示不同的Text。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); myCheck=(CheckBox)findViewById(R.id.checkBox1); myCheck.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO 自动生成的方法存根
setBulbState(isChecked);
}});
}
private void setBulbState(boolean isChecked) {
// TODO 自动生成的方法存根
img=(ImageView)findViewById(R.id.imageView1); img.setImageResource((isChecked)?R.drawable.bulbon:R.drawable.buldoff); myCheck=(CheckBox)findViewById(R.id.checkBox1);
myCheck.setText((isChecked)?R.string.offn:R.string.onn);
myCheck.setChecked(isChecked);
}
3.ToogleButton
ToogleButton俗称开关控件,可以分别设置它的EditTextOn和EditTextOff两个状态下的文字,对于该控件也需要添加监听的事件,获取控件的状态。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); myToggle=(ToggleButton)findViewById(R.id.toggleButton1); myToggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO 自动生成的方法存根
setBulbState(isChecked);
} }
private void setBulbState(boolean isChecked) {
// TODO 自动生成的方法存根
img=(ImageView)findViewById(R.id.imageView1); img.setImageResource((isChecked)?R.drawable.bulbon:R.drawable.buldoff); myToggle=(ToggleButton)findViewById(R.id.toggleButton1);
myToggle.setChecked(isChecked);
}
ToogleButton控件
4.Xml文件
Xml前台设置文件如下:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/button1"
android:layout_width="180dp"
android:layout_height="64dp"
android:layout_x="45dp"
android:layout_y="269dp"
android:background="@drawable/btn01"
android:text="Button" /> <ImageButton
android:id="@+id/imageButton1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_x="139dp"
android:layout_y="399dp"
android:background="@drawable/easyicon_net_24"
android:src="@drawable/imgbutton" /> <ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="145dp"
android:layout_y="211dp"
android:text="ToggleButton"
android:textOff="开灯"
android:textOn="关灯" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="77dp"
android:layout_height="77dp"
android:layout_x="30dp"
android:layout_y="84dp"
android:src="@drawable/buldoff" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="164dp"
android:layout_y="115dp"
android:text="@string/onn" /> <RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="51dp"
android:layout_y="182dp" > <RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="172dp"
android:layout_y="181dp"
android:text="关灯" /> <RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="36dp"
android:layout_y="201dp"
android:text="开灯" />
</RadioGroup> </AbsoluteLayout>
Xml文件
RadioButton、CheckBox与ToggleButton的更多相关文章
- 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch
原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, Rad ...
- 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch
[源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...
- 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch
1.ListBox 的示例Controls/SelectionControl/ListBoxDemo.xaml <Page x:Class="Windows10.Controls.Se ...
- Android用户界面 UI组件--TextView及其子类(五) DigitalClock,AnalogClock,RadioButton,CheckBox,ToggleButton汇总
DigitalClock和AnalogClock两个时钟类 可以为DigitalClock设置背景图片,自定义时针,秒针,分针的样式 例子: <?xml version="1.0&qu ...
- C# Windows - RadioButton&CheckBox
RadioButton和CheckBox控件与Button控件有相同的基类,但它们的外观和用法大不相同. RadioButton显示为一个标签,左边是一个圆点,该点可以是选中或未选中.用在给用户提供两 ...
- WPF RadioButton & CheckBox Style
<Style TargetType="CheckBox"> <Setter Property="Template"> <Sette ...
- android基本控件学习-----RadioButton&CheckBox
RadioButton(单选框)和CheckBox(复选框)讲解: 一.基本用法和事件处理 (1)RadioButton单选框,就是只能选择其中的一个,我们在使用的时候需要将RadioButton放到 ...
- Android 常用控件自定义样式RadioButton、CheckBox、ProgressBar、
一.RadioButton / CheckBox 系统自带的RadioButton/CheckBox的样式,注定满足不了实际运用中的情况,有时候自定义自己的样式:此次把自己中工作学习过程中所学到的东西 ...
- Android开发:文本控件详解——RadioButton和CheckBox(一)基本属性
一.RadioButton和RadioGroup: RadioButton是单个的圆形单选框,而RadioGroup是可以容纳多个RadioButton存在的容器,因此RadioButton和Radi ...
随机推荐
- iOS动画一点也不神秘————你是喜欢看幻灯片?还是看高清电影?
iOS设备在平均线上硬件比andorid设备良好许多,尤其是内存和CPU,所以iOS应用里面有大量动画交互效果的交互,这是每个用户都喜悦的,如果每个操作对应界面来讲都是直接变化,那变得十分地生硬. 你 ...
- css之background的cover和contain的缩放背景图
对于这两个属性,官网是这样解释的: contain 此时会保持图像的纵横比并将图像缩放成将适合背景定位区域的最大大小. 等比例缩放图象到垂直或者水平其中一项填满区域. cover 此时会保持图像的纵横 ...
- iOS应用之间的跳转与数据传递
在开发的时候遇到需要从其他APP调用自己的APP的需求,比如从Safari中打开APP,并且传递一些信息的需要 1.首先设置自己的URL types 打开项目中的工程文件,打开info选项,在下面的U ...
- 005 列表以及append,extend方法
定义一个列表: number=[,'changhao','常浩',5.2] 往这个列表里面添加单一新值(类型无限制),需要使用append方法. 例如: number.append() number. ...
- C语言中宏定义(#define)时do{}while(0)的价值(转)
C语言中宏定义(#define)时do{}while(0)的价值 最近在新公司的代码中发现到处用到do{...}while(0),google了一下,发现Stack Overflow上早有很多讨论,总 ...
- asp.net 获取当前url地址
设当前页完整地址是:http://www.jb51.net/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名 "www.jb5 ...
- git 快速使用(本地仓库同步到远程仓库)
学git一段时间,可惜公司用的是svn,平时少用,又忘了,总结一下,免得下次又得重新学习.得多多用才是正道! 一. 将本地的提交到网上git仓库 1.在git创建仓库 ...
- PHP解决网站高流量高并发问题
首先,确认服务器硬件是否足够支持当前的流量. 普通的P4服务器一般最多能支持每天10万独立IP,如果访问量比这个还要大, 那么必须首先配置一台更高性能的专用服务器才能解决问题 ,否则怎么优化都不可能彻 ...
- 使用notepad++学习python爬虫,print网页中文乱码问题
今天学习使用python爬虫的时候发现爬到的网页中文会乱码,一直网上搜索解决办法,一个一个试验过去,发现还是乱码,然后我就开始使用其它方法测试,用python自带的编辑器打开是正常的,发现是notep ...
- Java GC机制和对象Finalize方法的一点总结
GC是垃圾收集的意思(Garbage Collection),内存处理是编程人员容易出现问题的地方,忘记或者错误的内存回收会导致程序或系统的不稳定甚至崩溃,Java提供的GC功能可以自动监测对象是否超 ...