当前位置:网站首页>Togglebutton realizes the effect of switching lights
Togglebutton realizes the effect of switching lights
2022-07-06 11:54:00 【Forgotten world】
Please add group for problem consultation and project source code download :
Group name :IT Project communication group
Group number :245022761
【 experiment Purpose 】
1. master ToggleButton How to use .
【 Experimental principle 】
ToggleButton( Switch button ) Inherited from CompoundButton, It is a button with two statuses of selected and unselected , And different display text can be set for different states , Often used to indicate open / Off the scene .
【 Experimental content 】
This experimental case , adopt ToggleButton Realize the effect of a light switch . The effect is as shown in the picture 1 Shown .
Through the switch button below , Realize the effect of switching lights .
【 practice step 】
1. Create a new project
First create an empty project , Such as MyToggleButton project , Then make the following modifications .
2. Resource building
(1) picture
From the open 、 Picture of turning off the light , Store in res/drawable Under the table of contents , The file names are open.jpg、close.jpg.
3. Set the main layout file
Modify the main layout file activity_main.xml, This example uses the relative layout manager , Add to ImageView、ToggleBuuton Control , And set its related properties , The details are as follows .
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:id="@+id/img_light_on"
android:layout_marginTop="71dp"
android:src="@drawable/close"/>
<ToggleButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/toggleButton"
android:layout_marginTop="63dp"
android:layout_below="@+id/img_light_on"
android:text="ToggleButton"/>
</RelativeLayout>
4. Write code
The program code mainly realizes two Button Event handling method of , The specific code is as follows .
package com.example.mytogglebutton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private ToggleButton toggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.img_light_on);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
toggleButton.setTextOn(" Turn off the lights ");
toggleButton.setTextOff(" turn on the light ");
toggleButton.setChecked(false);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
imageView.setImageResource(R.drawable.open);
}else {
imageView.setImageResource(R.drawable.close);
}
}
});
}
}
5. Verification effect
Compile operation , By turning off the light / Switch button , Realize the effect of switching lights , The effect is as shown in the picture 1 Shown .
1.51 Single chip learning and sorting
be based on 51 Intelligent light controlled street lamp of single chip microcomputer :https://download.csdn.net/download/qq_37037348/11071869
be based on 51 SCM ultrasonic ranging ( Including source program , Schematic diagram and PCB Source file ):https://download.csdn.net/download/qq_37037348/11071866
be based on 51 Intelligent security alarm system of single chip microcomputer :https://download.csdn.net/download/qq_37037348/11071865
be based on 51 MCU modular programming module ( infrared 、 LCD screen 、 Temperature and humidity sensors are modularized )
:https://download.csdn.net/download/qq_37037348/11053222
be based on 51 Single chip microcomputer pwm Controlled breathing lamp program
https://download.csdn.net/download/qq_37037348/11053195
51 The example of serial port communication between MCU and host computer contains the complete code explained in detail
https://download.csdn.net/download/qq_37037348/11053164
be based on 51 The simulation of DC / AC voltmeter of single chip microcomputer ( Detailed code implementation , Design explanation )
https://download.csdn.net/download/qq_37037348/11053145
be based on 51 MCU badge Detailed code implementation , Design explanation )
https://download.csdn.net/download/qq_37037348/11053125
be based on 51 Single chip microcomputer 3x4 Key dialing ( Detailed code implementation , Design explanation )
https://download.csdn.net/download/qq_37037348/11053093
be based on 51 Single chip microcomputer dialing ( Detailed code implementation , Design explanation )
https://download.csdn.net/download/qq_37037348/11053090
be based on 51 Single chip microcomputer alarm system design ( Detailed code implementation , Design explanation )
https://download.csdn.net/download/qq_37037348/11053086
be based on 51 SCM lights up a small lamp ( Detailed code implementation , Design explanation , Study 51 Basic experiments )
https://download.csdn.net/download/qq_37037348/11053084
be based on 51 Volleyball timer developed by MCU , With detailed notes , To provide you with the most sincere help
https://download.csdn.net/download/qq_37037348/11053024
be based on 51 SCM music player , Source detailed notes
https://download.csdn.net/download/qq_37037348/11053022
2.Android Development, learning and sorting :
Android-serialport mobile phone App Serial communication code implementation :
https://download.csdn.net/download/qq_37037348/11050521
Android-serialport mobile phone App Network communication example code implementation :
https://download.csdn.net/download/qq_37037348/11050516
Android first App Detailed tutorial 、 Basic experiments :
https://download.csdn.net/download/qq_37037348/11050515
3. Computer vision ( Deep learning 、 Learning neural networks )
feature extraction( Deep learning , feature extraction , neural network :https://download.csdn.net/download/qq_37037348/11065968
feature extraction( Deep learning , feature extraction , Various training models of neural network are realized in detail ):
https://download.csdn.net/download/qq_37037348/11065974
Welcome to join the Learning Project Exchange , Share all kinds of personal learning projects and learning materials , Exchange and learn from each other .
Please add group for problem consultation and project source code download :
Group name :IT Project communication group
Group number :245022761
边栏推荐
- Composition des mots (sous - total)
- Implementation scheme of distributed transaction
- SQL time injection
- Variable star user module
- Gallery之图片浏览、组件学习
- MATLAB学习和实战 随手记
- Dependency in dependencymanagement cannot be downloaded and red is reported
- L2-001 emergency rescue (25 points)
- 2020 WANGDING cup_ Rosefinch formation_ Web_ nmap
- STM32型号与Contex m对应关系
猜你喜欢
Word排版(小计)
Implementation scheme of distributed transaction
wangeditor富文本引用、表格使用问题
Nanny level problem setting tutorial
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
Reading BMP file with C language
Small L's test paper
STM32型号与Contex m对应关系
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_ s instead
随机推荐
Reading BMP file with C language
Word排版(小計)
L2-006 tree traversal (25 points)
[Bluebridge cup 2020 preliminary] horizontal segmentation
Pytorch-温度预测
Basic use of pytest
Detailed explanation of nodejs
MySQL realizes read-write separation
B tree and b+ tree of MySQL index implementation
XML文件详解:XML是什么、XML配置文件、XML数据文件、XML文件解析教程
FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
2020网鼎杯_朱雀组_Web_nmap
Codeforces Round #771 (Div. 2)
Reading notes of difficult career creation
encoderMapReduce 随手记
yarn安装与使用
STM32型号与Contex m对应关系
第4阶段 Mysql数据库
Vert. x: A simple TCP client and server demo
Linux Yum install MySQL