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 ...
随机推荐
- Windows Mobile 6 sdk installation error, COM3 in use,please check the implementation
问题:Windows Mobile 6 sdk installation error, COM3 in use,please check the implementation 1. Windows-& ...
- Java中对象的三种状态
Java中的对象的三种状态是和垃圾回收紧密相关的,因此有必要深究. 状态一:可触及态:从根节点开始,可以搜索到这个对象,也就是可以访问到这个对象,也有人将其称为可达状态. 状态二:可复活态:从根节点开 ...
- 利用iptables将本地的80端口请求转发到8080,当前主机ip为192.168.1.1,命令怎么写?
iptables -t nat -A PREROUTING -d 192.168.1.1 -p tcp --dport 80 -j REDIRECT --to-port 8080 内网上外网: ipt ...
- 程序员使用Node的十个技巧
从问世到现在将近20年,JavaScript一直缺乏其它有吸引力的编程语言,比如Python和Ruby,的很多优点:命令行界面,REPL,包管理器,以及组织良好的开源社区.感谢Node.js和npm, ...
- 使用一个HttpModule拦截Http请求,来检测页面刷新(F5或正常的请求)
在Web Application中,有个问题就是:“我怎么来判断一个http请求到底是通过按F5刷新的请求还是正常的提交请求?” 相信了解ASP.NET的人知道我在说什么,会有同感,而且这其实不是一个 ...
- C#获取本机IP方法,获取本机局域网IP地址方法
1. private void GetIP() { string hostName = Dns.GetHostName();//本机名 //System.Net.IPAddress[] address ...
- 转JS技巧
前端已经被玩儿坏了!像console.log()可以向控制台输出图片等炫酷的玩意已经不是什么新闻了,像用||操作符给变量赋默认值也是人尽皆知的旧闻了,今天看到Quora上一个帖子,瞬间又GET了好多前 ...
- json 去空值与缩进
var jSetting = new Newtonsoft.Json.JsonSerializerSettings(); //忽略值为null的 jSetting.NullValueHandling ...
- stringstream函数(i o)
stringstream函数 头文件 #include<sstream> stringstream是字符串流,被用来切分数据或转化类型 样例一(摘) 输入n,代表接下来输入n行资料,每行 ...
- 关于js中window.location.href,location.href,parent.location.href,top.location.href的用法与区别(跳出iframe方法)
"window.location.href"."location.href"是本页面跳转 "parent.location.href"是上一 ...