复选框首选项,并检查其启用还是禁用 [英] Checkbox Preference and Checking if its enabled or disable
本文介绍了复选框首选项,并检查其启用还是禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的代码在设置复选框首选项时遇到问题,默认情况下处于选中状态.我的第一个活动是一个简单的启动屏幕,并且在显示我的imageview线程之前,我想检查复选框是否已禁用,如果是,那么我想直接参与主要活动,并且默认情况下,我显示我的图像线程,或者以相反的方式显示的顺序.
Im having issues with the below code on making a checkbox preference, That by default is checked. My first activity is a simple splash screen, and simply before show my imageview thread i want to check if the checkbox has been disable if so then i want to intent directly to the main activity and by default i show my image thread, or in reversed order of that.
当前我的启动画面正在启动,无论是否选中它,都将不胜感激
Currently my splashscreen is launching no matter if its checked or now, Any help would be greatly appreciated
<CheckBoxPreference
android:title="@string/category_tools_startupscreen"
android:summary="@string/category_tools_startupscreen_summary"
android:key="boot_animation" android:order="5" android:enabled="true"/>
SplashScreen
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)) {
.........
您甚至不需要为设置
这篇关于复选框首选项,并检查其启用还是禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文