<?xml version="1.0" encoding="utf-8"?>

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >                                            <!-- 声明一个线性布局 -->
  6. <ImageView
  7. android:id="@+id/ImageView01"
  8. android:src="@drawable/bulb_off"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_gravity="center_horizontal">
  12. </ImageView>                              <!-- 声明一个ImageView控件 -->
  13. <RadioGroup
  14. android:id="@+id/RadioGroup01"
  15. android:orientation="horizontal"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_gravity="center_horizontal">          <!-- 声明一个RadioGroup控件 -->
  19. <RadioButton
  20. android:text="@string/off"
  21. android:id="@+id/off"
  22. android:checked="true"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content">
  25. </RadioButton>                                <!-- 声明一个RadioButton控件 -->
  26. <RadioButton
  27. android:text="@string/on"
  28. android:id="@+id/on"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content">
  31. </RadioButton>                                <!-- 声明一个RadioButton控件 -->
  32. </RadioGroup>
  33. <CheckBox
  34. android:text="@string/on"
  35. android:id="@+id/CheckBox01"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_gravity="center_horizontal">
  39. </CheckBox>                                       <!-- 声明一个CheckBox控件 -->
  40. </LinearLayout>
  1. package com.ethan;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.CheckBox;
  5. import android.widget.CompoundButton;
  6. import android.widget.CompoundButton.OnCheckedChangeListener;
  7. import android.widget.ImageView;
  8. import android.widget.RadioButton;
  9. public class BulbActivity extends Activity {
  10. /** Called when the activity is first created. */
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. CheckBox cb=(CheckBox)this.findViewById(R.id.CheckBox01);
  16. cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){//为CheckBox添加监听器及开关灯业务代码
  17. @Override
  18. public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  19. setBulbState(isChecked);
  20. }
  21. });
  22. RadioButton rb=(RadioButton)findViewById(R.id.off);
  23. rb.setOnCheckedChangeListener(new OnCheckedChangeListener(){ //为RadioButton添加监听器及开关灯业务代码
  24. @Override
  25. public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  26. setBulbState(!isChecked);
  27. }
  28. });
  29. }
  30. //方法:设置程序状态的
  31. public void setBulbState(boolean state){
  32. //设置图片状态
  33. ImageView iv=(ImageView)findViewById(R.id.ImageView01);
  34. iv.setImageResource((state)?R.drawable.bulb_on:R.drawable.bulb_off);
  35. CheckBox cb=(CheckBox)this.findViewById(R.id.CheckBox01);
  36. cb.setText((state)?R.string.off:R.string.on);
  37. cb.setChecked(state);                           //设置复选框文字状态
  38. RadioButton rb=(RadioButton)findViewById(R.id.off);
  39. rb.setChecked(!state);
  40. rb=(RadioButton)findViewById(R.id.on);
  41. rb.setChecked(state);                           //设置单选按钮状态
  42. }
  43. }

源代码:http://115.com/file/dpfwj2k9

Android 之 电灯泡开关效果相关推荐

  1. 【Android自定义Dialog】电视机开关效果的Dialog

    前言: 今天没有什么工作要做,就准备网上找个源码解剖下,找些干货耍耍.在DEV资源网找到了这个http://www.devstore.cn/code/info/1144.html[Android音乐播 ...

  2. android togglebutton 动画,Android开发之ToggleButton实现开关效果示例

    本文实例讲述了Android使用ToggleButton实现开关效果的方法.分享给大家供大家参考,具体如下: activity_main.xml android:layout_width=" ...

  3. android 高仿ios开关,Android自定义view仿IOS开关效果

    本文主要讲解如何在 Android 下实现高仿 iOS 的开关按钮,并非是在 Android 自带的 ToggleButton 上修改,而是使用 API 提供的 onDraw.onMeasure.Ca ...

  4. html switch开关实现隐藏,css3实现switch开关效果

    之前阿里电面的时候问的一个问题,今天抽时间做了个demo. html结构 css代码,:before负责颜色,:after是那个白色小圆点,切换时的过渡效果用css3的动画实现. .switch{ d ...

  5. Android横向伸缩,Android 实现伸缩布局效果示例代码

    最近项目实现下面的图示的效果,本来想用listview+gridview实现,但是貌似挺麻烦的于是就用flowlayout 来addview实现添加伸缩的效果,实现也比较简单. mainActivit ...

  6. Android实现左右滑动效果

    本示例演示在Android中实现图片左右滑动效果.   关于滑动效果,在Android中用得比较多,本示例实现的滑动效果是使用ViewFlipper来实现的,当然也可以使用其它的View来实现.接下来 ...

  7. Android 卡片翻转动画效果

    转载请标明出处:http://blog.csdn.net/android_mnbvcxz/article/details/78570594 Android 卡片翻转动画效果 前言 前端时间开发一款应用 ...

  8. android 3d渲染动画效果吗,Android如何实现3D效果

    前言 前段时间读到一篇文章,作者通过自定义View实现了一个高仿小米时钟,其中的3D效果很是吸引我,于是抽时间学习了一下,现在总结出来,和大家分享. 正文 想要在Android上实现3D效果,其实并没 ...

  9. Android 实现ListView圆角效果

     今天,简单讲讲如何实现使用  ListView显示圆角. 其实代码很多都可以解决,这是在网上搜索的一个解决的代码. 无论是网站,还是APP,人们都爱看一些新颖的视图效果.直角看多了,就想看看 ...

最新文章

  1. 相互靠近的色环电感之间的互感
  2. 用WindowManager实现Android悬浮框以及拖动事件
  3. 力扣交替打印FooBar
  4. java jni helloword_JNI学习一:编写HelloWorld程序
  5. UVA216 ——dfs
  6. V - 不容易系列之(4)――考新郎(第二季水)
  7. 从前端开发者看待用友建筑云移动端单点登录与报错原因
  8. MVC项目开发中那些用到的知识点(Ajax.BeginForm)
  9. 乘客网上订票系统MVC
  10. matlab在脚本中用函数,matlab中脚本和函数的怎么调用
  11. android多点触摸事件,Android多点触控
  12. 消费者太穷不愿买手机?苹果的份额创新高,撕下国产手机遮羞布
  13. 锂电池电量百分比计算_锂电池容量计算
  14. 12306抢票JS脚本
  15. reverse和reversed函数的总结
  16. 零代码搭建一个温度传感器数据采集与显示软件
  17. 内核通知链(网络子系统为例)
  18. 一个简单的用户登录界面
  19. mysql y m d h_php时间问题?mysql数据库的时间格式(Y-M-D H:I:S) 在PHP页面想这样显示(Y-M-D) ('.$rows['ndate'].')...
  20. 语音处理:PCM文件中采样值到dB分贝的转换分析

热门文章

  1. 关于Oracle函数INSTR使用的问题
  2. 如何有效理清对象间逻辑关系?XMind鱼骨图帮你轻松搞定!
  3. Python:画一只小狗
  4. Windows下为磁盘添加病毒免疫文件
  5. 原始字符串(如换行符或 Unicode 字符)
  6. iOS 14中人工智能的进步
  7. 华为更新后计算机,华为手机升级到鸿蒙系统后,如果后悔了,怎样退回到EMUI系统?...
  8. 高德地图推出酒店比价,为爱牵线后能为盈利搭桥吗?
  9. 计算机毕业设计开题报告的撰写步骤和内容要求
  10. C-小d和超级泡泡堂