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

 

原网站

版权声明
本文为[Forgotten world]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060913166535.html