我有一个类,我有一个选项按钮,打开一个包含6个复选框的对话框。默认情况下,首次加载应用时,4个复选框设置为true。用户可以选中并取消选中这些复选框。当我按下后退按钮时,对话框消失,如果再次单击“选项”按钮,则不会根据所选的最后一个状态选中/取消选中复选框。我使用共享首选项来保存这些值。
我的Java类代码在这里。我删除了下面不必要的方法,并发布了xml布局。请帮助解决共享偏好无效的原因。
GlamDokuActivity
public class GlamDokuActivity extends Activity{
private Button easy,fair,hard,evil,insane;
private CheckBox screenOn,clashSquares,quickNotes,soundOn,showTimer,matchingNumbers;
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;
public static final String DefaultChoicesOn = "defaults";
public static final String Timer ="true";
public static final String QuickNotes ="true";
public static final String ScreenOn ="false";
public static final String ClashingSquares ="true";
public static final String SoundOn ="false";
public static final String MatchingNumbers ="true";
//keeping a tab on the status of the above default options
private boolean timerStatus=false,quickNotesStatus=false,screenOnOffStatus=false,clashingSquaresStatus=false,soundOnOffStatus=false,matchingNumberStatus=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glamdoku_main);
//call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
sharedpreferences=getSharedPreferences(GlamDokuActivity.DefaultChoicesOn, Context.MODE_PRIVATE);
//saving in the shared preferences
editor = sharedpreferences.edit();
//If the key is not present then only add the key-value pair
if(!sharedpreferences.contains(Timer))editor.putBoolean(GlamDokuActivity.Timer, true);
if(!sharedpreferences.contains(ClashingSquares))editor.putBoolean(GlamDokuActivity.ClashingSquares, true);
if(!sharedpreferences.contains(MatchingNumbers))editor.putBoolean(GlamDokuActivity.MatchingNumbers, true);
if(!sharedpreferences.contains(SoundOn))editor.putBoolean(GlamDokuActivity.SoundOn, false);
if(!sharedpreferences.contains(ScreenOn))editor.putBoolean(GlamDokuActivity.ScreenOn, false);
if(!sharedpreferences.contains(QuickNotes))editor.putBoolean(GlamDokuActivity.QuickNotes, true);
editor.commit();
//KEEP THE SCREEN ON
if(sharedpreferences.getBoolean(ScreenOn, true))getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//System.out.println("I AM IN ONCREATE OF ");
}//onCreate() ends
public void optionsMethod(View v){//options button click method
//dialog that appears when the hints button is clicked
final Dialog dialog = new Dialog(GlamDokuActivity.this,R.style.dialogStyle);
dialog.setContentView(R.layout.layout_options);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(true);
dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
dialog.show();
//tagging the ids
screenOn = (CheckBox)dialog.findViewById(R.id.screenOn);
clashSquares = (CheckBox)dialog.findViewById(R.id.clash);
quickNotes = (CheckBox)dialog.findViewById(R.id.quickNotes);
soundOn = (CheckBox)dialog.findViewById(R.id.soundOnOff);
showTimer = (CheckBox)dialog.findViewById(R.id.timer);
matchingNumbers = (CheckBox)dialog.findViewById(R.id.matchNumbers);
//default values from shared preferences --- depending upon these the state of the respective checkboxes to change
timerStatus=sharedpreferences.getBoolean(GlamDokuActivity.Timer, true);
quickNotesStatus=sharedpreferences.getBoolean(GlamDokuActivity.QuickNotes, true);
screenOnOffStatus=sharedpreferences.getBoolean(GlamDokuActivity.ScreenOn, false);
clashingSquaresStatus=sharedpreferences.getBoolean(GlamDokuActivity.ClashingSquares, true);
soundOnOffStatus=sharedpreferences.getBoolean(GlamDokuActivity.SoundOn, false);
matchingNumberStatus=sharedpreferences.getBoolean(GlamDokuActivity.MatchingNumbers, true);
//check or uncheck depending upon the aforementioned boolean values
if(timerStatus==false)showTimer.setChecked(false); else showTimer.setChecked(true);
if(quickNotesStatus==false)quickNotes.setChecked(false);else quickNotes.setChecked(true);
if(screenOnOffStatus==false)screenOn.setChecked(false);else screenOn.setChecked(true);
if(clashingSquaresStatus==false)clashSquares.setChecked(false);else clashSquares.setChecked(true);
if(soundOnOffStatus==false)soundOn.setChecked(false);else soundOn.setChecked(true);
if(matchingNumberStatus==false)matchingNumbers.setChecked(false);else matchingNumbers.setChecked(true);
//-----------------------------------------------------------------------------------------------------------------
//click the screenOn chkbox
screenOn.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,1));
//click the clashing squares chkbox
clashSquares.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,2));
//click the quick notes chkbox
quickNotes.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,3));
//click the sound chkbox
soundOn.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,4));
//click the show timer chkbox
showTimer.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,5));
//click the matching numbers chkbox
matchingNumbers.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,6));
@Override
public void onResume(){
//KEEP THE SCREEN ON
if(sharedpreferences.getBoolean(ScreenOn, true))getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onResume();
}
}
CommonCheckClickListener
public class CommonCheckClickListener implements OnClickListener {
private Dialog dialog;
private SherlockActivity activity;
private int thisOption;
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;
public CommonCheckClickListener(Dialog dialog, Activity activity, int thisOption){
this.activity=activity;
this.dialog=dialog;
this.thisOption=thisOption;
//call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
sharedpreferences=activity.getSharedPreferences(GlamDokuActivity.DefaultChoicesOn, Context.MODE_PRIVATE);
editor=sharedpreferences.edit();
}
@Override
public void onClick(View v) {
int flag=0;
if(((CheckBox)v).isChecked())flag=1; else flag=0;//flagged as checked
switch(thisOption){
case 1: //screenOn option
if(flag==1){
editor.putBoolean(GlamDokuActivity.ScreenOn, true);
Toast.makeText(activity, "Warning - Keeping the screen always on consumes battery.", Toast.LENGTH_LONG).show();
}
else{
editor.putBoolean(GlamDokuActivity.ScreenOn, false);
}
editor.commit();
boolean screenOn = sharedpreferences.getBoolean(GlamDokuActivity.ScreenOn, true);
break;
case 2: //clashing squares option
if(flag==1)editor.putBoolean(GlamDokuActivity.ClashingSquares, true);else editor.putBoolean(GlamDokuActivity.ClashingSquares, false);
editor.commit();
boolean cs = sharedpreferences.getBoolean(GlamDokuActivity.ClashingSquares, true);
break;
case 3: //quick notes option
if(flag==1)editor.putBoolean(GlamDokuActivity.QuickNotes, true); else editor.putBoolean(GlamDokuActivity.QuickNotes, false);
editor.commit();
boolean qn = sharedpreferences.getBoolean(GlamDokuActivity.QuickNotes, true);
break;
case 4: //sound option
if(flag==1)editor.putBoolean(GlamDokuActivity.SoundOn, true); else editor.putBoolean(GlamDokuActivity.SoundOn, false);
editor.commit();
boolean sound = sharedpreferences.getBoolean(GlamDokuActivity.SoundOn, true);
break;
case 5: //show Timer option
if(flag==1)editor.putBoolean(GlamDokuActivity.Timer, true); else editor.putBoolean(GlamDokuActivity.Timer, false);
editor.commit();
boolean timer = sharedpreferences.getBoolean(GlamDokuActivity.Timer, true);
break;
case 6: //matching numbers option
if(flag==1)editor.putBoolean(GlamDokuActivity.MatchingNumbers, true); else editor.putBoolean(GlamDokuActivity.MatchingNumbers, false);
editor.commit();
boolean matching = sharedpreferences.getBoolean(GlamDokuActivity.MatchingNumbers, true);
break;
}
}
main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
>
<Button
style="@style/button_main_screen"
android:id="@+id/playsudoku"
android:onClick="playMethod"
android:text="@string/playsudoku" />
<Button
style="@style/button_main_screen"
android:id="@+id/options"
android:onClick="optionsMethod"
android:text="@string/optionssudoku" />
</LinearLayout>
Dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical" >
<CheckBox
android:id="@+id/matchNumbers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginTop="21dp"
android:text="@string/match_numbers" />
<CheckBox
android:id="@+id/clash"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clash_squares" />
<CheckBox
android:checked="true"
android:id="@+id/quickNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/quick_notes" />
<CheckBox
android:id="@+id/timer"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/timer_always" />
<CheckBox
android:id="@+id/screenOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:text="@string/screen_always" />
<CheckBox
android:id="@+id/soundOnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="25dp"
android:text="@string/sound_off" />
</LinearLayout>
答案 0 :(得分:1)
静态键变量的定义不正确。他们需要是其他名称而不是“真实”和“虚假”。更改初始化值后,问题就解决了。 - Ganesh Hegde
答案 1 :(得分:0)
您应该使用OnCheckedChangeListener
而非OnClickListener
作为CheckBoxes。 OnClickListener#onClick
之前调用CheckBox
已检查状态已更改。