AS手电筒变色效果

三个功能按钮健(红色,蓝色、黄色):
点击某个按钮背景显示相应胡颜色

新建AS项目

项目结构基本如下只需更改标记的文件

Activity_Main.xml

<?xml version="1.0" encoding="utf-8"?>
  <androidx.constraintlayout.widget.ConstraintLayout 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">
  
      <TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="8dp"
          android:text="欢迎使用手电筒程序!"
          android:textAppearance="@style/TextAppearance.AppCompat.Large"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.154" />
  
      <Button
          android:id="@+id/b"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="295dp"
          android:layout_marginEnd="16dp"
          android:layout_marginRight="16dp"
          android:onClick="onClick"
          android:text="蓝色"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toStartOf="@+id/y"
          app:layout_constraintStart_toEndOf="@+id/r"
          app:layout_constraintTop_toBottomOf="@+id/textView"
          app:layout_constraintVertical_bias="0.0" />
  
      <Button
          android:id="@+id/y"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="38dp"
          android:layout_marginLeft="38dp"
          android:layout_marginTop="295dp"
          android:layout_marginEnd="36dp"
          android:layout_marginRight="36dp"
          android:onClick="onClick"
          android:text="黄色"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toEndOf="@+id/b"
          app:layout_constraintTop_toBottomOf="@+id/textView"
          app:layout_constraintVertical_bias="0.0" />
  
      <Button
          android:id="@+id/r"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="44dp"
          android:layout_marginLeft="44dp"
          android:layout_marginTop="295dp"
          android:layout_marginEnd="29dp"
          android:layout_marginRight="29dp"
          android:onClick="onClick"
          android:text="红色"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toStartOf="@+id/b"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/textView"
          app:layout_constraintVertical_bias="0.0" />
  
  </androidx.constraintlayout.widget.ConstraintLayout>
  

MainActivity.java

package com.davis.flashlight;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.graphics.Color;
  import android.graphics.drawable.ColorDrawable;
  import android.os.Bundle;
  import android.view.View;
  
  public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      public void onClick(View v) {
          switch (v.getId()) {
              case R.id.r:
                  getWindow().setBackgroundDrawable(new ColorDrawable(Color.RED));
                  break;
                  case R.id.b:
                      getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLUE));
                      break;
                      case R.id.y:
                          getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
                          break;		}
      }
  
  }