開新檔案-
布置一下design-
放入兩張電燈照片當開關-
Firebase網站連線-
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);
}
}
燈是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);
}
}
開關燈的狀態
按鈕要綁onclick
明天就要第一次的全職面試了~好緊張啊~"後來錄取沒去上班"
(我印象中我上次投履歷是在疫情前2020/3..難道等了一年半履歷才被看0.0,不管啦~為了下次還有面試機會要來亂投~投越多面試機會越大~順便把我的年假+生理假消一消~不然放著沒用好可惜0.0~今天投了一些發現都是跟前端/PHP/C#/JAVA/Android有關~然後就算面試上也是要要持續進修~聽說有人到當天還被取消錄取~或是無法通過試用期)
好吧~
來寫寫C#中跟Android相同的部分吧~
3.相等是兩個==不相等是!=
4.如果是已經宣告的int型別X,無法再將X宣告為布林
5.在BUTTON按鈕到這裡是跟C#不一樣,id是控制項的名稱,供程式中呼叫使用/Text藉由程式控制指定文字
6.長的像這樣的UI的程式碼
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();
}
}
希望...可以讓我繼續有鐵人發文的機會~拜託拜託