6

我在制作复选框首选项时遇到以下代码问题,默认情况下已选中。我的第一个活动是一个简单的启动屏幕,就在显示我的 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;
                }
            }); 
4

1 回答 1

9

在您的启动画面中

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

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

您甚至不需要为设置发布的代码

于 2013-06-06T07:01:17.233 回答