如果没有默认设置,如何设置共享首选项以显示警告框。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* RR - Restore Prefrences */
SharedPreferences settings =
this.getSharedPreferences("com.sample.test",Context.MODE_PRIVATE);
Boolean isBackgroundWhite = settings.getBoolean("BgColor", true);
//triggerAlert();
}
答案 0 :(得分:4)
使用SharedPreferences.contains()
测试是否已设置首选项。如果没有,请显示您的对话框。
if(!settings.contains("BgColor")){
// Make a call to your dialog code here.
}
答案 1 :(得分:1)
当用户第一次启动应用程序时,使用SharedPreferences.contains检查是否包含SharedPreference包含密钥:
SharedPreferences settings =
this.getSharedPreferences("com.sample.test",Context.MODE_PRIVATE);
if (settings.contains("BgColor")) {
// means key is present in preference
//so no need to show settings
}else{
// key not present in preference so
// store key in preference and show setting
}
答案 2 :(得分:0)
if (this.getSharedPreferences("com.sample.test",Context.MODE_PRIVATE).getBoolean
("firstLaunch", true)){ //Display your dialog settings
this.getSharedPreferences("com.sample.test",Context.MODE_PRIVATE).edit.
setBoolean ("firstLaunch", false).commit;
}