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