Я пытался реализовать стиль темы для нескольких действий с помощью Utils. java и я создал другое действие с основным действием, которое называется темами, поскольку я могу поместить в него кнопки для управления всей темой приложения с помощью кнопок my Utils. java
import android.app.Activity;
import android.content.Intent;
public class Utils {
private static int sTheme;
public final static int THEME_DEFAULT = 0;
public final static int THEME_DARK = 1;
public final static int THEME_GREEN = 2;
public final static int THEME_YELLOW = 3;
public final static int THEME_PINK = 4;
public final static int THEME_RED = 5;
public final static int THEME_GREY = 6;
public static void changeToTheme(Activity activity, int theme)
{
sTheme = theme;
activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()));
}
/** Set the theme of the activity, according to the configuration. */
public static void onActivityCreateSetTheme(Activity activity)
{
switch (sTheme)
{
default:
case THEME_DEFAULT:
activity.setTheme(R.style.AppTheme);
break;
case THEME_DARK:
activity.setTheme(R.style.darktheme);
break;
case THEME_GREEN:
activity.setTheme(R.style.theme1);
break;
case THEME_YELLOW:
activity.setTheme(R.style.theme2);
break;
case THEME_PINK:
activity.setTheme(R.style.theme3);
break;
case THEME_RED:
activity.setTheme(R.style.theme4);
break;
case THEME_GREY:
activity.setTheme(R.style.theme5);
break;
}
}
}
мои темы активности. java
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Themes extends AppCompatActivity implements OnClickListener {
public static final String SHARED_PREFS = "sharedPrefs";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.onActivityCreateSetTheme(this);
setContentView(R.layout.activity_themes);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
findViewById(R.id.button6).setOnClickListener(this);
findViewById(R.id.button7).setOnClickListener(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int UI_OPTIONS = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
getWindow().getDecorView().setSystemUiVisibility(UI_OPTIONS);
}
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
public void onBack(View v) {
super.onBackPressed(); // or super.finish();
}
public void finish() {
super.finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Utils.changeToTheme(this, Utils.THEME_DEFAULT);
break;
case R.id.button2:
Utils.changeToTheme(this, Utils.THEME_DARK);
break;
case R.id.button3:
Utils.changeToTheme(this, Utils.THEME_GREEN);
break;
case R.id.button4:
Utils.changeToTheme(this, Utils.THEME_YELLOW);
break;
case R.id.button5:
Utils.changeToTheme(this, Utils.THEME_PINK);
break;
case R.id.button6:
Utils.changeToTheme(this, Utils.THEME_RED);
break;
case R.id.button7:
Utils.changeToTheme(this, Utils.THEME_GREY);
break;
}
}
}
мой макет активности activity_themes. xml
<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"
android:background="@drawable/themes2"
android:orientation="vertical"
tools:context=".Themes">
<Button
android:id="@+id/button7"
android:layout_width="136dp"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="42dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="279dp"
android:text="Seventh Theme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/button1"
app:layout_constraintTop_toBottomOf="@+id/button5" />
<Button
android:id="@+id/button5"
android:layout_width="136dp"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="37dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="61dp"
android:text="Fifth Theme"
app:layout_constraintBottom_toTopOf="@+id/button7"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/button1"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button4"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="37dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="406dp"
android:text="Fourth Theme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button1" />
<Button
android:id="@+id/button6"
android:layout_width="136dp"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="406dp"
android:text="Sixs Theme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/button3" />
<Button
android:id="@+id/btnback"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@android:color/transparent"
android:gravity="center"
android:onClick="onBack"
android:shadowColor="?attr/shadcolor"
android:shadowDx="2"
android:shadowDy="2"
android:shadowRadius="1.5"
android:text="⏪"
android:textColor="?attr/textcolor"
android:textSize="25dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button1"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="126dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="37dp"
android:text="First Theme"
app:layout_constraintBottom_toTopOf="@+id/button6"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnback" />
<Button
android:id="@+id/button2"
android:layout_width="136dp"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="192dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="37dp"
android:text="Second Theme"
app:layout_constraintBottom_toTopOf="@+id/button5"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintStart_toEndOf="@+id/button1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button3"
android:layout_width="136dp"
android:layout_height="wrap_content"
android:layout_marginTop="192dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="37dp"
android:text="Third Theme"
app:layout_constraintBottom_toTopOf="@+id/button6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
что я должен добавить к мои другие действия, поэтому, когда я нажимаю любую кнопку темы, чтобы изменить другие действия с этим действием?