android - 复选框首选项和检查是否启用或禁用

标签 android sharedpreferences splash-screen android-preferences android-checkbox

我在设置复选框首选项时遇到以下代码问题,默认情况下已选中。我的第一个 Activity 是一个简单的启动画面,在显示我的 imageview 线程之前,我想检查复选框是否已被禁用,如果是,那么我想直接进入主要 Activity ,默认情况下我显示我的图像线程,或者相反的顺序。

目前我的闪屏正在启动,无论是选中还是现在,任何帮助将不胜感激

XML

 <CheckBoxPreference
      android:title="@string/category_tools_startupscreen"
      android:summary="@string/category_tools_startupscreen_summary"
      android:key="boot_animation" android:order="5" android:enabled="true"/>

启动画面

   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

    if (settings.getBoolean("boot_animation", true)) {
        setContentView(R.layout.splash_screen);
        Thread splashThread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    // changed from 5000 to 4000 11.29
                    while (waited < 3000) {
                        sleep(100);
                        waited += 100;
                    }
                } catch (InterruptedException e) {
                    // do nothing
                } finally {
                    Intent i = new Intent();
                    i.setClassName("com.example.app",
                            "com.example.app.MainActivity");
                    startActivity(i);
                    finish();
                }
            }
        };
        splashThread.start();
    }

    else {
        Intent i = new Intent();
        i.setClassName("com.example.app",
                "com.example.app.MainActivity");
        startActivity(i);
        finish();
    }

}

设置

    final CheckBoxPreference checkboxPref2 = (CheckBoxPreference) getPreferenceManager().findPreference("boot_animation");

            checkboxPref2.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
                 public boolean onPreferenceChange(Preference preference, Object newValue) {
                if(newValue instanceof Boolean){
                        Boolean boolVal = (Boolean)newValue;
                        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putBoolean("checked", boolVal);
                        editor.commit();
                    }
                    return true;
                }
            }); 

最佳答案

在启动画面中

PreferenceManager.setDefaultValues(this, R.xml.your_setting_xml, false);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

if (settings.getBoolean("boot_animation", true)) {
.........

您甚至不需要为设置

发布的代码

关于android - 复选框首选项和检查是否启用或禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16955443/

相关文章:

java - 为什么我无法在手机中安装应用程序。 Redmi 7s 已连接,但在 Android Studio 中未显示安装应用程序。为什么?

android - 如何控制启动 Intent 时显示的应用程序列表?

java - 如何访问JNLP中指定的启动画面?

ios - 如何更改 Xamarin IOS 启动画面上的版权信息和日期?

android - flutter 图像自动幻灯片/轮播,给定随机间隔

android - 微调器删除项目

java - Android 共享首选项

android - 通过共享首选项保存 "if-statements"和统计信息

Android:根据用户是否登录更改 Activity 启动我的应用程序?

java - 安卓启动屏幕