Android ToggleButton(开关函数)与switch (开关按钮)
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)效果图

Android ToggleButton(开关函数)与switch (开关按钮)的更多相关文章
- Android学习笔记-开关按钮ToggleButton和开关Switch
本节给大家介绍的Android基本UI控件是:开关按钮ToggleButton和开关Switch,这两个其实都是开关组件,只是后者需要在Android 4.0以后才能使用 所以AndroidManif ...
- android开关控件Switch和ToggleButton
序:今天项目中用到了开关按钮控件,查阅了一些资料特地写了这篇博客记录下. 1.Switch <Switch android:id="@+id/bt" android:layo ...
- Android——复选按钮和开关按钮
复选按钮和开关按钮代码如下: <LinearLayout android:layout_width="match_parent" android:layout_height= ...
- Android课程---视图组件之开关按钮
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...
- Android ToggleButton Example--开关按钮
Android ToggleButton Example 在 Android 中, “android.widget.ToggleButton” 是个特殊的类,可以渲染出一个“开关按钮” ,顾名思义, ...
- Android内核sysfs中switch类使用实例
Android内核sysfs中switch类使用实例 最终在这个周末,能够干点自己想要干的事了. 由我这个二流的内核驱动开发人员来解析一下sysfs中的switch类.先猜測一下来历,在普通的嵌入式L ...
- Android ToggleButton:状态切换的Button
Android ToggleButton:状态切换的Button Android ToggleButton和Android Button类似,但是ToggleButton提供了一种选择机制,可以 ...
- Android基础控件ToggleButton和Switch开关按钮
1.简介 ToggleButton和Switch都是开关按钮,只不过Switch要Android4.0之后才能使用! ToggleButton <!--checked 是否选择--> &l ...
- 状态开关按钮(ToggleButton)和开关(Switch)
ToggleButton支持的XML属性及相关方法1.android:checked----->setChecked(boolean) ----->设置该按钮是否被选中2.android: ...
随机推荐
- T-SQL 理解SQL SERVER中的分区表(转)
转载来源一定要明显: http://www.cnblogs.com/CareySon/archive/2011/12/30/2307766.html 而且这个大神对于数据库方面的文章非常棒 强烈推荐 ...
- Elasticsearch聚合限制内存使用
限制内存使用 通常为了让聚合(或者任何需要访问字段值的请求)能够快点,访问fielddata一定会快点, 这就是为什么加载到内存的原因.但是加载太多的数据到内存会导致垃圾回收(gc)缓慢, 因为JVM ...
- orzdba工具安装注意事项
orzdba是一个监控mysql性能的一个比较好用的perl脚本,是淘宝开源的小工具,下载地址http://code.taobao.org/p/orzdba/src/trunk/ 配置过程中除了参照& ...
- SpringMVC——数据转换 & 数据格式化 & 数据校验
一.数据绑定流程 1. Spring MVC 主框架将 ServletRequest 对象及目标方 法的入参实例传递给 WebDataBinderFactory 实例,以创 建 DataBinder ...
- JavaScript跨域调用基于JSON的RESTful API
1. 基本术语 AJAX(Asynchronous JavaScript And XML,异步JavaScript和XML):AJAX是一种用于创建快速动态网页的技术,通过在后台与服务器进行少量数据交 ...
- Java学习第六篇:集合类
一.Java集合类框架 Java集合大致可分为Set.List和Map三种体系,其中Set代表无序.不可重复的集合:List代表有序.重复的集合:而Map则代表具有映射关系的集合:从Java5以后,J ...
- 软工作业WordCount
github项目传送门:https://github.com/zzh010/My-wc 一.WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命 ...
- delphi窗体启动外部exe
uses Winapi.Windows; WinExec(PAnsiChar(Application.ExeName), sw_normal); // PAnsiChar : string to ...
- Go语言最佳实践——通道和并发
何时关闭通道: 第一,只有在后面要检查通道是否关闭的时候才需要显式地关闭通道: 第二,应该由发送端的goroutine关闭通道,而不是由接收端的goroutine来完成: 第三,如果通道并不需要检查是 ...
- php统计目录大小
function dirSize($directroy) { $dir_size=0; $dir_handle = @opendir($directroy); if($dir_handle) { wh ...