这似乎是一个经常被问到的问题,但我仍然无法使用Stack Overflow上提出的非常方法。我有一个preference.xml如下:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:defaultValue="true"
android:key="isBlock"
android:layout="@layout/custom_checkbox_layout"
android:summaryOff="Let strange calls in"
android:summaryOn="Unlisted calls will be blocked"
android:title="Block Strange Calls" />
.....
我希望在代码中更改复选框的状态。所以我的Java代码发布在下面:
public class Prefs extends PreferenceActivity implements OnSharedPreferenceChangeListener {
public static boolean blockOrNot;
private MySharedPreference mySharedPreference;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
setContentView(R.layout.custom_preference_layout);
ImageButton save = (ImageButton) findViewById(R.id.save_prefs);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (blockOrNot) {
Toast.makeText(Prefs.this, "Saved! " + blockOrNot, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Prefs.this, "Saved! " + blockOrNot, Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
blockOrNot = sp.getBoolean("isBlock", false);
}
@Override
protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// TODO Auto-generated method stub
if (key.equals("isBlock")) {
blockOrNot = sharedPreferences.getBoolean(key, false);
}
}
}
但每当我按下按钮时,我只能得到“true”,这是复选框的原始值,我以为我取消选中它。我搜索这个问题,然后在SOF上试过一种方法,如下所示:
public class Prefs extends PreferenceActivity implements OnSharedPreferenceChangeListener {
public static boolean blockOrNot;
private MySharedPreference mySharedPreference;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
setContentView(R.layout.custom_preference_layout);
ImageButton save = (ImageButton) findViewById(R.id.save_prefs);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (blockOrNot) {
Toast.makeText(Prefs.this, "Saved! " + blockOrNot, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Prefs.this, "Saved! " + blockOrNot, Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onResume() {
super.onResume();
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, final String key) {
// TODO Auto-generated method stub
CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference(key);
checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (key.equals("isBlock")) {
blockOrNot = (Boolean) newValue;
}
return true;
}
});
}
}
但仍然没有改变。有人可以请一些建议吗?谢谢大家!!