当前位置: 首页 > 知识库问答 >
问题:

日/夜模式在初始启动时不工作

任飞鸣
2023-03-14
dayNightMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            System.out.println("Not Checked");
            InitApplication.getInstance().setIsNightModeEnabled(true, context);
            Intent intent = getIntent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            finish();
            startActivity(intent);
            dayNightMode.setText("Night Mode");
        } else {
            System.out.println("Checked");
            InitApplication.getInstance().setIsNightModeEnabled(false, context);
            Intent intent = getIntent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            finish();
            startActivity(intent);
            dayNightMode.setText("Day Mode");
        }
    }
});
package com.dmurphy.remotescrumpoker;

    import android.app.Application;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;
    import android.util.Log;

    public class InitApplication extends Application {
        public static final String NIGHT_MODE = "NIGHT_MODE";
        private boolean isNightModeEnabled = false;

        private static InitApplication singleton = null;

        public static InitApplication getInstance() {

            if(singleton == null)
            {
                singleton = new InitApplication();
            }
            return singleton;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            singleton = this;
            Context appContext = this.getApplicationContext();

            SharedPreferences sharedPreferences = appContext.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
            isNightModeEnabled = sharedPreferences.getBoolean(NIGHT_MODE, true);
            Log.i("onCreate1_", String.valueOf(isNightModeEnabled));
        }

        public boolean isNightModeEnabled(Context context) {
            SharedPreferences sharedPreferences = context.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
            isNightModeEnabled = sharedPreferences.getBoolean(NIGHT_MODE, true);
            Log.i("onCreate11_", String.valueOf(isNightModeEnabled));
            return isNightModeEnabled;
        }

        public void setIsNightModeEnabled(boolean isNightModeEnabled, Context context) {
            this.isNightModeEnabled = isNightModeEnabled;
            Log.i("onCreate111_", String.valueOf(isNightModeEnabled));

            SharedPreferences sharedPreferences = context.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
            sharedPreferences.edit().putBoolean(NIGHT_MODE, isNightModeEnabled).apply();
            Log.i("onCreate1111_", String.valueOf(isNightModeEnabled));
        }
    }

我还有一个activity_poker活动。一切都很好,如果我改变开关,应用程序就会改变。

我还有一个启动活动activity_splash,在oncreate中,我检查模式:

if (InitApplication.getInstance().isNightModeEnabled(context)) {
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    Log.i(TAG, "onCreate1_" + "Mode Night Yes");
} else {
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    Log.i(TAG, "onCreate1_" + "Mode Night No");
}
setContentView(R.layout.activity__splash);

初创公司运转良好。当我关闭应用程序并重新启动时,背景色等对两种模式都是正确的。我在活动上有3个可抽屉可见。这些也在两种替代模式之间发生了细微的变化。

public class Activity_Poker extends AppCompatActivity {

[snip]
        Activity passedActivity = Activity_Poker.this;
[snip]
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_poker);
            drawerLayout = findViewById(R.id.drawer_layout);
    [snip]
            final String methodName = "onCreate";
            mAuth = FirebaseAuth.getInstance();
            String userId = mAuth.getUid();
    [snip]
            //Setup the UI for the selected game type
            int cardType = sharedPreferences.getInt("cardType", 0);
            Log.i(TAG, "Selected SharedPref Record: " + cardType);
            callSetupUI.setupLocalActive(passedActivity, cardType);
    [snip]
}

塞图普伊·爪哇

    public void setupLocalActive(Activity passedActivity, int cardType) {

        ImageView card4 = passedActivity.findViewById(R.id.pokerCard4);
        ImageView card4img = passedActivity.findViewById(R.id.pokerCard4img);
        ImageView card5 = passedActivity.findViewById(R.id.pokerCard5);
        ImageView card5img = passedActivity.findViewById(R.id.pokerCard5img);
        ImageView card6 = passedActivity.findViewById(R.id.pokerCard6);
        ImageView card6img = passedActivity.findViewById(R.id.pokerCard6img);
        ImageView card7 = passedActivity.findViewById(R.id.pokerCard7);
        ImageView card7img = passedActivity.findViewById(R.id.pokerCard7img);
        ImageView card8 = passedActivity.findViewById(R.id.pokerCard8);
        ImageView card8img = passedActivity.findViewById(R.id.pokerCard8img);
        ImageView card9 = passedActivity.findViewById(R.id.pokerCard9);
        ImageView card9img = passedActivity.findViewById(R.id.pokerCard9img);
        ImageView card11 = passedActivity.findViewById(R.id.pokerCard11);
        ImageView card11img = passedActivity.findViewById(R.id.pokerCard11img);

        TextView card4TextView = passedActivity.findViewById(R.id.textViewPokerCard4);
        TextView card5TextView = passedActivity.findViewById(R.id.textViewPokerCard5);
        TextView card6TextView = passedActivity.findViewById(R.id.textViewPokerCard6);
        TextView card7TextView = passedActivity.findViewById(R.id.textViewPokerCard7);
        TextView card8TextView = passedActivity.findViewById(R.id.textViewPokerCard8);
        TextView card9TextView = passedActivity.findViewById(R.id.textViewPokerCard9);
        TextView card10TextView = passedActivity.findViewById(R.id.textViewPokerCard10);
        TextView card11TextView = passedActivity.findViewById(R.id.textViewPokerCard11);
        TextView card12TextView = passedActivity.findViewById(R.id.textViewPokerCard12);
        TextView card13TextView = passedActivity.findViewById(R.id.textViewPokerCard13);
        TextView card14TextView = passedActivity.findViewById(R.id.textViewPokerCard14);
        TextView card15TextView = passedActivity.findViewById(R.id.textViewPokerCard15);

        Log.i(TAG, "Selected SharedPref Record UI CardType into Switch: " + cardType);

        switch (cardType){
            case 1:
[snip]
            case 2:
[snip]
            case 3:
[snip]
            case 4:
[snip]
            case 5:
                Log.i(TAG, "Selected SharedPref Record Setting UI: " + cardType);
                card10.setVisibility(card10.INVISIBLE);
                card12.setVisibility(card12.INVISIBLE);
                card13.setVisibility(card13.INVISIBLE);
                card14.setVisibility(card14.INVISIBLE);
                card15.setVisibility(card15.INVISIBLE);
                card4TextView.setVisibility(card4TextView.INVISIBLE);
                card5TextView.setVisibility(card5TextView.INVISIBLE);
                card6TextView.setVisibility(card6TextView.INVISIBLE);
                card7TextView.setVisibility(card7TextView.INVISIBLE);
                card8TextView.setVisibility(card8TextView.INVISIBLE);
                card9TextView.setVisibility(card9TextView.INVISIBLE);
                card10TextView.setVisibility(card10TextView.INVISIBLE);
                card11TextView.setVisibility(card10TextView.INVISIBLE);
                card12TextView.setVisibility(card12TextView.INVISIBLE);
                card13TextView.setVisibility(card13TextView.INVISIBLE);
                card14TextView.setVisibility(card14TextView.INVISIBLE);
                card15TextView.setVisibility(card15TextView.INVISIBLE);

                card4img.setImageResource(R.drawable.chihuahua);
                card5img.setImageResource(R.drawable.dachshund);
                card6img.setImageResource(R.drawable.beagle);
                card7img.setImageResource(R.drawable.spaniel);
                card8img.setImageResource(R.drawable.boxer);
                card9img.setImageResource(R.drawable.shepard);
                card11img.setImageResource(R.drawable.dane);

                card4img.setVisibility(card4img.VISIBLE);
                card5img.setVisibility(card5img.VISIBLE);
                card6img.setVisibility(card6img.VISIBLE);
                card7img.setVisibility(card7img.VISIBLE);
                card8img.setVisibility(card8img.VISIBLE);
                card9img.setVisibility(card9img.VISIBLE);
                card11img.setVisibility(card11img.VISIBLE);

                break;

            default:
[snip]
                break;
        }

    }

activity_poker.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="260dp">

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="8dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="32dp">

[snip]
        <ImageView
            android:id="@+id/pokerCard5"
            android:layout_width="88dp"
            android:layout_height="104dp"
            android:layout_marginTop="16dp"
            android:background="@color/colorPrimary"
            android:contentDescription="@string/localCardSelectcontent"
            android:onClick="card8clicked"
            app:layout_constraintEnd_toStartOf="@+id/pokerCard6"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/pokerCard4"
            app:layout_constraintTop_toBottomOf="@+id/pokerCard2" />

        <ImageView
            android:id="@+id/pokerCard5img"
            android:layout_width="72dp"
            android:layout_height="88dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:contentDescription="@string/localCardSelectcontent"
            android:onClick="localcard5clicked"
            android:visibility="invisible"
            app:layout_constraintStart_toStartOf="@+id/pokerCard5"
            app:layout_constraintTop_toTopOf="@+id/pokerCard5"
            app:srcCompat="@drawable/dachshund" />

        <TextView
            android:id="@+id/textViewPokerCard5"
            android:layout_width="88dp"
            android:layout_height="104dp"
            android:onClick="localcard5clicked"
            android:gravity="center"
            android:includeFontPadding="false"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="44sp"
            android:textStyle="normal|bold"
            app:layout_constraintStart_toStartOf="@+id/pokerCard5"
            app:layout_constraintTop_toTopOf="@+id/pokerCard5" />

        <ImageView
            android:id="@+id/pokerCard4"
            android:layout_width="88dp"
            android:layout_height="104dp"
            android:background="@color/colorPrimary"
            android:contentDescription="@string/localCardSelectcontent"
            android:onClick="card5clicked"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="@+id/pokerCard5"
            app:layout_constraintEnd_toStartOf="@+id/pokerCard5"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent" />

        <ImageView
            android:id="@+id/pokerCard4img"
            android:layout_width="72dp"
            android:layout_height="88dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:contentDescription="@string/localCardSelectcontent"
            android:onClick="localcard4clicked"
            android:visibility="invisible"
            app:layout_constraintStart_toStartOf="@+id/pokerCard4"
            app:layout_constraintTop_toTopOf="@+id/pokerCard4"
            app:srcCompat="@drawable/chihuahua" />

        <TextView
            android:id="@+id/textViewPokerCard4"
            android:layout_width="88dp"
            android:layout_height="104dp"
            android:onClick="localcard4clicked"
            android:gravity="center"
            android:includeFontPadding="false"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="44sp"
            android:textStyle="normal|bold"
            app:layout_constraintStart_toStartOf="@+id/pokerCard4"
            app:layout_constraintTop_toTopOf="@+id/pokerCard4" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:onClick="onSwitchChange"
    app:headerLayout= "@layout/nav_header_main"
    app:menu="@xml/drawer_view" >

</com.google.android.material.navigation.NavigationView>

共有1个答案

吕嘉荣
2023-03-14

这可能不是正确的解决方案,但我实现了一个变通方法。

activity_splash中,我添加了一个bool来指示它是初始启动。

SharedPreferences sharedPreferences = Activity_Splash.this.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
sharedPreferences.edit().putBoolean("initialStartup", true).apply();

在Activity_Poker中,我检索了bool并且如果NIGHT_MODE被启用并且“initial startup”为true,我将重新启动Activity_Poker以重置UI。

boolean initialStartup = sharedPreferences.getBoolean("initialStartup", false);
if (InitApplication.getInstance().isNightModeEnabled(context) && initialStartup) {

    callUserMenu.menuAction(TAG, methodName, context, 5, passedActivity); //This triggers my Intent to restart the activity
    sharedPreferences.edit().remove("initialStartup").apply();
}
 类似资料:
  • 我按照步骤创建了HelloWorld示例,但它没有运行。它给出了以下错误: 初始化引导层java.lang.module.FindException时出错:读取模块:F:\Develop\Eclipse\HelloWorld\bin时出错,原因是:java.lang.module.InvalidModuleDescriptoRexception:HelloWorld.class在顶级目录中找到(模

  • 现在我们提供一个夜间模式,你只需要在 body 或者 .page 或者 .content 上加上 .theme-dark。它和其中所有子元素都会变成夜间模式。你也可以单独给 .bar 加上 .theme-dark,这样可以单独使标题栏或者工具栏变成夜间模式。 夜间模式最大的区别是他的背景变成了黑色,而前景色变成了白色。 夜间模式还处在测试阶段,可能会有某些组件在夜间模式下显示不正常。有任何问题都可

  • 问题内容: 我想在Web应用程序启动时加载属性文件并设置连接池。显然,我只想在一个地方进行一次,因此可以根据需要进行更改。对于常规的servlet,我只需将初始化代码放在servlet的init()方法中,但是您无法使用Jersey servlet对其进行访问。那么我在哪里做呢?上面链接中的侦听器如何工作? 问题答案: 您需要做的就是编写一个实现ServletContextListener接口的J

  • 我是一个非常好的noob,所以我想我正在用twitter引导模式来监督一些事情(可能很明显)。我想做的是让一个模态只在手机上启动。这在添加类时效果很好。modal div上的可视电话。到目前为止还不错。但我希望它能工作,这意味着你可以用X键关闭它。我不能让按钮工作。 在HTML的底部,我把jquery.js(第一)和bootstrap.modal.jsbootstrap.transition.js

  • 所以我最近在我的win10 PC上安装了eclipse,并创建了一个公共类。代码如下: 接下来,显示了一个错误: 然后,我删除了info.java文件,并显示以下错误: 我能做些什么来解决这个问题?

  • 我在电脑上安装了JDK版本15。我刚刚开始使用Java的Eclipse,我的程序都不工作。当我输入示例时 当我运行它时,它说:启动层初始化过程中发生错误java.lang.module.FindExcture: ModuleSimpleJavaProgram未找到 请告诉我我应该怎么做才能让我的程序正常工作?谢谢你, 萨拉