复选框首选项并检查其是启用还是禁用

时间:2013-06-06 06:46:36

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

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

目前我的启动画面无论是检查还是现在启动,任何帮助都将非常感谢

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;
                }
            }); 

1 个答案:

答案 0 :(得分:8)

在启动画面

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

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

您甚至不需要为设置

发布的代码