当前位置:网站首页>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
边栏推荐
猜你喜欢
MTCNN人脸检测
ToggleButton实现一个开关灯的效果
分布式事务的实现方案
4、安装部署Spark(Spark on Yarn模式)
Redis interview questions
2019腾讯暑期实习生正式笔试
About string immutability
[CDH] cdh5.16 configuring the setting of yarn task centralized allocation does not take effect
4. Install and deploy spark (spark on Yan mode)
Connexion sans mot de passe du noeud distribué
随机推荐
2020 WANGDING cup_ Rosefinch formation_ Web_ nmap
L2-007 family real estate (25 points)
encoderMapReduce 随手记
SQL time injection
【yarn】CDP集群 Yarn配置capacity调度器批量分配
Some concepts often asked in database interview
Niuke novice monthly race 40
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
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
Reading notes of difficult career creation
Basic use of pytest
库函数--(持续更新)
MySQL与c语言连接(vs2019版)
Word typesetting (subtotal)
[Presto] Presto parameter configuration optimization
Mtcnn face detection
Nanny level problem setting tutorial
Vs2019 first MFC Application
[BSidesCF_2020]Had_ a_ bad_ day
【Flink】CDH/CDP Flink on Yarn 日志配置