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的更多相关文章

  1. 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch

    原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, Rad ...

  2. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    [源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...

  3. 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    1.ListBox 的示例Controls/SelectionControl/ListBoxDemo.xaml <Page x:Class="Windows10.Controls.Se ...

  4. Android用户界面 UI组件--TextView及其子类(五) DigitalClock,AnalogClock,RadioButton,CheckBox,ToggleButton汇总

    DigitalClock和AnalogClock两个时钟类 可以为DigitalClock设置背景图片,自定义时针,秒针,分针的样式 例子: <?xml version="1.0&qu ...

  5. C# Windows - RadioButton&CheckBox

    RadioButton和CheckBox控件与Button控件有相同的基类,但它们的外观和用法大不相同. RadioButton显示为一个标签,左边是一个圆点,该点可以是选中或未选中.用在给用户提供两 ...

  6. WPF RadioButton & CheckBox Style

    <Style TargetType="CheckBox"> <Setter Property="Template"> <Sette ...

  7. android基本控件学习-----RadioButton&CheckBox

    RadioButton(单选框)和CheckBox(复选框)讲解: 一.基本用法和事件处理 (1)RadioButton单选框,就是只能选择其中的一个,我们在使用的时候需要将RadioButton放到 ...

  8. Android 常用控件自定义样式RadioButton、CheckBox、ProgressBar、

    一.RadioButton / CheckBox 系统自带的RadioButton/CheckBox的样式,注定满足不了实际运用中的情况,有时候自定义自己的样式:此次把自己中工作学习过程中所学到的东西 ...

  9. Android开发:文本控件详解——RadioButton和CheckBox(一)基本属性

    一.RadioButton和RadioGroup: RadioButton是单个的圆形单选框,而RadioGroup是可以容纳多个RadioButton存在的容器,因此RadioButton和Radi ...

随机推荐

  1. python中的tab补全功能添加

    用Python时没有tab补全还是挺痛苦的,记录一下添加该功能的方法利人利己 1. 先准备一个tab.py的脚本 shell> cat tab.py #!/usr/bin/python # py ...

  2. S - 骨牌铺方格(第二季水)

    Description          在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.         例如n=3时,为2× 3方格,骨牌的铺放方案有三种, ...

  3. STM32的例程GPIO的汇编指令初探

    任务一:寻找main函数的汇编指令集 任务二:寻找main函数中的SystemClock_Config函数的汇编指令集 寻找main函数的汇编指令集 运行例程中GPIO工程时,总会加载startup_ ...

  4. commands - `ls`

    list only directories: ls -d /path/to/*/

  5. [Oracle]查看和修改连接数

    #登陆数据库sqlplus system/*** as sysdba #显示当前最大连接数:show parameter processes; show parameter sessions; #修改 ...

  6. oracle add_months函数的用法详解

    如果需要取上一个月的数据,并且每天都要进行此操作,每次都需要改时间,的确非常的麻烦,所以想到了oracle add_months函数这个函数 oracle add_months函数: oracle a ...

  7. python运维开发(八)----面向对象(下)

    内容目录: 面向对象三大特性之多态性 面向对象中的成员:字段.方法.属性 类的成员修饰符 类的特殊成员 特殊成员方法 面向对象其他 异常处理 设计模式之单例模式 面向对象的多态性 多态性:即指多种形态 ...

  8. Python2.7 转义和正则匹配中文

    今天爬虫(新浪微博 个人信息页面)的时候遇到了转义和正则匹配中文出乱码的问题. 先给出要匹配的部分网页源代码如下: <span class=\"pt_title S_txt2\&quo ...

  9. 九章算法系列(#5 Linked List)-课堂笔记

    前言 又是很长时间才回来发一篇博客,前一个月确实因为杂七杂八的事情影响了很多,现在还是到了大火燃眉毛的时候了,也应该开始继续整理一下算法的思路了.Linked List大家应该是特别熟悉不过的了,因为 ...

  10. SQL Server 分区表的创建方法与管理

    背景知识: 分区表.可以把表中的数据按范围保存到不同的文件组中. 举个例子吧: 2014年以前的数据保存到文件组A 2014~2015的数据保存到文件组B 2015年以后的数据保存到文件组C 好处: ...