当前位置:网站首页>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

边栏推荐
猜你喜欢

电商数据分析--薪资预测(线性回归)

Vs2019 first MFC Application

Stage 4 MySQL database

Come and walk into the JVM
C语言读取BMP文件

Composition des mots (sous - total)

Apprentissage automatique - - régression linéaire (sklearn)

Mall project -- day09 -- order module

FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation

MongoDB
随机推荐
vs2019 第一个MFC应用程序
Correspondence between STM32 model and contex M
5G工作原理详解(解释&图解)
[Kerberos] deeply understand the Kerberos ticket life cycle
使用LinkedHashMap实现一个LRU算法的缓存
XML file explanation: what is XML, XML configuration file, XML data file, XML file parsing tutorial
機器學習--線性回歸(sklearn)
MongoDB
机器学习--决策树(sklearn)
Word排版(小計)
Encodermappreduce notes
快来走进JVM吧
Funny cartoon: Programmer's logic
天梯赛练习集题解LV1(all)
Machine learning -- decision tree (sklearn)
库函数--(持续更新)
Reading notes of difficult career creation
Principle and implementation of MySQL master-slave replication
PyTorch四种常用优化器测试
MySQL数据库面试题