第26天~用电灯的照片代表连到感应器

开新档案-

https://ithelp.ithome.com.tw/upload/images/20220204/20119035PDEVZm1ZgS.png


布置一下design-
放入两张电灯照片当开关-

https://ithelp.ithome.com.tw/upload/images/20220204/201190358OEkCIHQyR.png


Firebase网站连线-

https://ithelp.ithome.com.tw/upload/images/20220204/20119035YBY6Jw7YJr.png

https://ithelp.ithome.com.tw/upload/images/20220204/201190351pI7qUnyC1.png

package com.huang.mylight;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {

    //宣告
    TextView light_show;
    ImageButton mylight;
    Context context;
    boolean state;
    EditText message;

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("light");
    DatabaseReference myMsg = database.getReference("message");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

https://ithelp.ithome.com.tw/upload/images/20220204/20119035PKUn6od5z8.png

灯是Imagebutton


所有的程序码

package com.huang.mylight;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {

    //宣告
    TextView light_show;
    ImageButton mylight;
    Context context;
    boolean state;
    EditText message;

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("light");
    DatabaseReference myMsg = database.getReference("message");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;
        light_show = findViewById(R.id.light_show);
        mylight = findViewById(R.id.MyLight);

        //当按下图片进行切换状态
        mylight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                state = !state;
                if(state){
                    myRef.setValue(true);//firebase
                    mylight.setImageResource(R.drawable.lighton);//app
                    light_show.setText("开灯中");
                }else{
                    myRef.setValue(false);//firebase
                    mylight.setImageResource(R.drawable.lightoff);//app
                    light_show.setText("已关灯");
                }
            }
        });
        //取得Firebase目前的资料
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                state = snapshot.getValue(Boolean.class);
                if(state) {
                    mylight.setImageResource(R.drawable.lighton);//app
                    light_show.setText("开灯中");
                } else{
                    mylight.setImageResource(R.drawable.lightoff);//app
                    light_show.setText("已关灯");
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                light_show.setText("FAIL");
            }
        });
        //文字留言
        message = findViewById(R.id.message);
        myMsg.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                String mm = snapshot.getValue(String.class);
                message.setText(mm);
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                message.setText("FAIL");
            }
        });
    }
    //送出文字留言-->Fisebase
    public void onclick(View view) {
        String msg = message.getText().toString().trim();
        myMsg.setValue(msg);
    }
}

开关灯的状态

https://ithelp.ithome.com.tw/upload/images/20220204/20119035qFF2LVx1pe.jpg

https://ithelp.ithome.com.tw/upload/images/20220204/20119035HxU0hbiZik.jpg

按钮要绑onclick

https://ithelp.ithome.com.tw/upload/images/20220204/20119035UANyPUSqt3.png

https://ithelp.ithome.com.tw/upload/images/20220204/20119035rQuV1NoJKX.png

https://ithelp.ithome.com.tw/upload/images/20220204/20119035wP2hfqeijo.png


明天就要第一次的全职面试了~好紧张啊~"後来录取没去上班"
(我印象中我上次投履历是在疫情前2020/3..难道等了一年半履历才被看0.0,不管啦~为了下次还有面试机会要来乱投~投越多面试机会越大~顺便把我的年假+生理假消一消~不然放着没用好可惜0.0~今天投了一些发现都是跟前端/PHP/C#/JAVA/Android有关~然後就算面试上也是要要持续进修~听说有人到当天还被取消录取~或是无法通过试用期)

好吧~/images/emoticon/emoticon10.gif
来写写C#中跟Android相同的部分吧~

  1. 运算顺序由内到外:()->[]
  2. 运算顺序由左到右:*乘法->/除法->%取余数

3.相等是两个==不相等是!=
4.如果是已经宣告的int型别X,无法再将X宣告为布林
5.在BUTTON按钮到这里是跟C#不一样,id是控制项的名称,供程序中呼叫使用/Text藉由程序控制指定文字
https://ithelp.ithome.com.tw/upload/images/20211011/20119035eqUPkr5DKu.png

6.长的像这样的UI的程序码
https://ithelp.ithome.com.tw/upload/images/20211011/20119035hQwIPNoJ4h.png

package com.huang.myapp01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
    //宣告变数
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;
    String str1, str2, str3;

    //初始化元件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = findViewById(R.id.input);
        input.setText("NO INPUT");
        str1 = "toggleButton-->NO";
        str2 = "switch1-->NO";
        str3 = input.getText().toString();

        toggleButton = findViewById(R.id.toggleButton);
        switch1 = findViewById(R.id.switch1);
        //toggleButton赋予功能
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
                    str1 = "toggleButton-->OK";
                }
                else{
                    Toast.makeText(MainActivity.this, "NO", Toast.LENGTH_SHORT).show();
                    str1 = "toggleButton-->NO";
                }
            }
        });
        //switch1赋予功能
        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
                    str2 = "switch1-->OK";
                }
                else{
                    Toast.makeText(MainActivity.this, "NO", Toast.LENGTH_SHORT).show();
                    str2 = "switch1-->OK";
                }
            }
        });
    }
    //透过 button 收集全部的资料
    public void onClick(View view) {
        str3 = input.getText().toString();
        Toast.makeText(MainActivity.this,
                str1+"\n"+str2+"\n"+str3,
                Toast.LENGTH_LONG).show();
    }
}

希望...可以让我继续有铁人发文的机会~拜托拜托


<<:  DAY29: 最後倒数

>>:  EP29 - 秽土转生~到了 AWS 也要能够备份~

#23 No-code 之旅 — Next.js 网站可以部署到哪里呢?

大家~ 今天文章会也会很短Q 昨天讲了静态网站可以部署到哪里之後,今天来讲 Next.js 的专案可...

Day10 跟着官方文件学习Laravel-Migration

Migration 是资料库的版本控制,让你和你的团队能够互相去共想资料库的结构,你是否曾经曾告诉你...

[day-11] 一切的基础! Python "运算式与算符"的运用(Part .1)

一、何谓运算式?   所谓的运算式是指『运算资料的式子』,其中代表着运算行为的符号称为 『算符』 ,...

Unity - 互动功能(终章)

按钮 ( Button ) 执行 [ GameObject > UI > Button ...

数据操作语言(Data manipulation language)

这个问题描述了常见的SQL注入场景,该场景采用了像1 = 1这样的所谓“身份方程序”。攻击者可以输入...