diff --git a/.idea/misc.xml b/.idea/misc.xml index 0ad17cb..8978d23 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index 6637e1b..09f6885 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -1,12 +1,19 @@ package com.example.childguard; +import android.content.SharedPreferences; +import android.graphics.Color; +import android.graphics.drawable.Drawable; import android.os.Bundle; +import androidx.core.content.res.ResourcesCompat; import androidx.fragment.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.FrameLayout; +import android.widget.TextView; /** * A simple {@link Fragment} subclass. @@ -61,4 +68,26 @@ public class HomeFragment extends Fragment { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_home, container, false); } + + @Override + public void onResume() { + super.onResume(); + Log.d("HomeFragment", "onResume: called"); + TextView situationTextView = getView().findViewById(R.id.situation); + FrameLayout situation_bg=getView().findViewById(R.id.situation_bg); + updateInCarStatus(situationTextView,situation_bg); + } + + public void updateInCarStatus(TextView situationTextView,FrameLayout situation_bg) { + SharedPreferences sharedPreferences = getActivity().getSharedPreferences("default", 0); + + Log.d("HomeFragment", "updateInCarStatus: " + sharedPreferences.getBoolean("inCar", false)); + if (sharedPreferences.getBoolean("inCar", false)) { + situationTextView.setText("\n降車状態"); + situation_bg.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style, null)); + } else { + situationTextView.setText("\n乗車状態"); + situation_bg.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.frame_style_orange, null)); + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/childguard/QR.java b/app/src/main/java/com/example/childguard/QR.java new file mode 100644 index 0000000..ebdf156 --- /dev/null +++ b/app/src/main/java/com/example/childguard/QR.java @@ -0,0 +1,33 @@ +package com.example.childguard; + +import android.graphics.Color; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.widget.TextView; + +import androidx.appcompat.app.AppCompatActivity; + +public class QR extends AppCompatActivity { + String get_on="乗車状態"; + String get_off ="降車状態"; + TextView tv=findViewById(R.id.situation); + @Override + protected void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + setContentView(R.layout.fragment_qr); + + findViewById(R.id.camera).setOnClickListener( + v -> { + if(get_on.equals(tv.getText().toString())){ + tv.setText(get_off); + findViewById(R.id.situation_bg).setBackgroundColor(Color.parseColor("#dcdcdc")); + } + else { + tv.setText(get_on); + findViewById(R.id.situation_bg).setBackgroundColor(Color.parseColor("#ff4500")); + } + } + ); + } + +} diff --git a/app/src/main/java/com/example/childguard/QRFragment.java b/app/src/main/java/com/example/childguard/QRFragment.java index 4321325..7e26151 100644 --- a/app/src/main/java/com/example/childguard/QRFragment.java +++ b/app/src/main/java/com/example/childguard/QRFragment.java @@ -1,12 +1,15 @@ package com.example.childguard; +import android.content.SharedPreferences; import android.os.Bundle; import androidx.fragment.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; /** * A simple {@link Fragment} subclass. @@ -53,12 +56,33 @@ public class QRFragment extends Fragment { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } + + } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_qr, container, false); + View view = inflater.inflate(R.layout.fragment_qr, container, false); + + Button cameraButton = view.findViewById(R.id.camera); + + // Init shared preferences + SharedPreferences sharedPreferences = getActivity().getSharedPreferences("default", 0); + SharedPreferences.Editor editor = sharedPreferences.edit(); + + cameraButton.setOnClickListener(v -> { + Log.d("QRFragment", "onClick: called"); + if (!sharedPreferences.getBoolean("inCar", false)) { + editor.putBoolean("inCar", true); + } else { + editor.putBoolean("inCar", false); + } + editor.apply(); + }); + + return view; } + } \ No newline at end of file diff --git a/app/src/main/res/drawable/frame_style_orange.xml b/app/src/main/res/drawable/frame_style_orange.xml new file mode 100644 index 0000000..3b4922f --- /dev/null +++ b/app/src/main/res/drawable/frame_style_orange.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index a3ec0de..21d6c78 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -7,8 +7,8 @@ android:layout_height="match_parent" android:orientation="vertical" tools:context=".HomeFragment"> - - +