iT邦幫忙

2021 iThome 鐵人賽

DAY 26
0
Mobile Development

就是從無到有寫app系列 第 26

第26天~用電燈的照片代表連到感應器

  • 分享至 

  • xImage
  •  

開新檔案-

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();
    }
}

希望...可以讓我繼續有鐵人發文的機會~拜託拜託


上一篇
第25天~還是Firebase
下一篇
第27天~CRUD
系列文
就是從無到有寫app31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言