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. Android实战技巧之十九:android studio导出jar包(Module)并获得手机信息

    AS中并没有独立的Module 工程,可是能够在普通的Project中增加Module.所谓的Module就是我们通常所指的模块化的一个单元.并经常以jar包的形式存在.以下以一个获取手机信息的样例演 ...

  2. 理解java设计模式之观察者模式

    在生活实际中,我们经常会遇到关注一个事物数据变化的情况,例如生活中的温度记录仪,当温度变化时,我们观察它温度变化的曲线,温度记录日志等.对于这一类问题,很接近java设计模式里面的“观察者模式”,它适 ...

  3. SqlServer2008数据库透明加密

    前几天研究了一下sql数据库的透明加密,记下来加深一下理解. 用脚本创建文件夹 --查文件夹有没有 EXEC master.dbo.xp_fileexist 'D:\DATA\storedcerts' ...

  4. 动态分配内存补充 realloc

    当再次在原来申请的内存基础上再加内存的时候用realloc,如果第一次分配的内存后面存储地方够用,则连着原来的申请,如果不够用,就重新找到一块够用的地方,然后把原来的复制过去 int main(int ...

  5. MVC思想架构的简单自定义UITableViewCell

         在iOS的开发过程中,架构思想是很重要的一部分,目前的主流应该分为MVC与MVVM两种,在这里不做过多的区分,有兴趣的同学可以看看唐巧大神的一篇文章<被误解的MVC和被神化的MVVM& ...

  6. .NET c# 串口通信

    这段时间做了一个和硬件设备通信的小项目,涉及到扫描头.输送线.称重机.贴标机等硬件.和各设备之间通信使用的是串口或网络(Socket)的方式.扫描头和贴标机使用的网络通信,输送线和称重机使用的是串口通 ...

  7. override和new的区别

    override 1. override是派生类用来重写基类中方法的: 2. override不能重写非虚方法和静态方法: 3. override只能重写用virtual.abstract.overr ...

  8. maven命令/依赖/聚合

    一,编写pom.xml   首先我们看一下pom.xml   <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...

  9. A + B Problem II---hdu1002

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  10. asp.net MVC4 +MVCpager 无刷新分页

    本人菜鸟,最近在用MVC4和MVCpager做无刷新分页时,发现点击下一页时数据不是Ajax提交的,弄了好久终于找到原因,原来还是Jquery引用的问题,现在把代码粘出来,希望能帮到刚接触的程序员,第 ...