当前位置:网站首页>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
边栏推荐
- [Kerberos] deeply understand the Kerberos ticket life cycle
- L2-004 is this a binary search tree? (25 points)
- MySQL数据库面试题
- [Presto] Presto parameter configuration optimization
- 2019 Tencent summer intern formal written examination
- 第4阶段 Mysql数据库
- MySQL realizes read-write separation
- [NPUCTF2020]ReadlezPHP
- 4. Install and deploy spark (spark on Yan mode)
- Library function -- (continuous update)
猜你喜欢
Linux Yum install MySQL
{one week summary} take you into the ocean of JS knowledge
[template] KMP string matching
IOT system framework learning
[yarn] CDP cluster yarn configuration capacity scheduler batch allocation
分布式节点免密登录
Machine learning -- linear regression (sklearn)
Composition des mots (sous - total)
Redis面试题
机器学习--决策树(sklearn)
随机推荐
Linux yum安装MySQL
2019腾讯暑期实习生正式笔试
MySQL realizes read-write separation
Connexion sans mot de passe du noeud distribué
【flink】flink学习
Vs2019 use wizard to generate an MFC Application
[Flink] cdh/cdp Flink on Yan log configuration
vs2019 第一个MFC应用程序
TypeScript
人脸识别 face_recognition
Codeforces Round #771 (Div. 2)
PHP - whether the setting error displays -php xxx When PHP executes, there is no code exception prompt
Detailed explanation of nodejs
Internet protocol details
Codeforces Round #753 (Div. 3)
[Blue Bridge Cup 2017 preliminary] buns make up
使用LinkedHashMap实现一个LRU算法的缓存
[Bluebridge cup 2020 preliminary] horizontal segmentation
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries(XGBoost)
JS array + array method reconstruction