1.ToggleButton

(1)介绍

(2)组件形状

(3)xml文件设置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <!--设置背景颜色,ToggleButton 默认灰色背景看不出来-->
<ToggleButton
android:id="@+id/toggleButton2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="ToggleButton"
android:background="#000fff"
android:textOff="关灯"
android:textOn="开灯" /> <ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="300dp"
app:srcCompat="@mipmap/img1" /> </LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="20dp"
android:background="#345555"
android:orientation="horizontal"> <Switch
android:id="@+id/switch1"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_weight="1"
android:text="客厅灯开关" />
</LinearLayout>
</LinearLayout>

(4)后台代码

对应的工程名:test22

package com.lucky.test22;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.ToggleButton; public class MainActivity extends AppCompatActivity { ToggleButton toggleButton;
ImageView imageView;
Switch aSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton=findViewById(R.id.toggleButton2);
aSwitch=findViewById(R.id.switch1);
imageView=findViewById(R.id.imageView);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
imageView.setImageResource(R.mipmap.img1); //修改所显示的图片
}else {
imageView.setImageResource(R.mipmap.img2);
}
}
}); aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
imageView.setImageResource(R.mipmap.img1); //修改所显示的图片
}else {
imageView.setImageResource(R.mipmap.img2);
}
}
});
}
}

(5)效果图

最新文章

  1. 创建APPID&amp;&amp;部署服务端教程
  2. 【.net 深呼吸】连接Access数据库应注意的几点
  3. 设计模式(六)原型模式(Prototype Pattern)
  4. 苹果IPhone手机由于更新了IOS7 Beta测试版导致“激活出错”后,如何还原电话本和照片方法
  5. Linux下巧用my.cnf,mysql连接服务器不需要输入账号密码信息
  6. Task多线程
  7. 新建一个mybatis HelloWorld
  8. Node.js学习笔记(1)
  9. 单独调用Ueditor的图片上传功能
  10. IIS 无法打开页面,只能重启的问题
  11. 【FOI】异或问题
  12. Codeforces Round #226 (Div. 2)C. Bear and Prime Numbers
  13. Ubuntu下给Sublime Text 3添加用python3运行文件
  14. Windows200864位操作系统下的SQLPLUS.EXE 无法找到入口解决办法和Oracle环境变量的设置
  15. 什么是jquery
  16. 前后端分离djangorestframework——restful规范
  17. HDU 1028(数字拆分 分治)
  18. jdbc与TiDB数据库交互的过程
  19. 浅谈websocket和c# socket(新手篇)
  20. css 按钮凹陷的感觉

热门文章

  1. Python_14-绘图
  2. 如何在CentOS里切换操作系统所用的语言,中英文切换
  3. 在线创建MongoDB免费集群(MangoDB Atlas)
  4. cv 验证
  5. 实现一个简单的Plugin
  6. 实践作业3:白盒测试----findbugs介绍及使用DAY7
  7. WCF把书读薄(2)——消息交换、服务实例、会话与并发
  8. App测试从入门到精通之更新测试
  9. Python基础入门-IF语句
  10. mvc全局过滤器和httpmodule的执行顺序