我在实施首选项时遇到问题。当我运行我的应用程序并单击首选项选项卡时,没有任何反应。我不知道代码有什么问题,因为 logcat 不显示任何内容。并提前感谢。
public class Reset extends PreferenceActivity{
private Preference myPreference;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myPreference = findPreference("reset");
myPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference arg0) {
Builder alertDialog = new AlertDialog.Builder(Reset.this);
alertDialog.setMessage("Reset?");
alertDialog.setCancelable(true);
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
} });
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
} });
alertDialog.show();
return false;
}
});
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference android:title="Reset Score" android:key="reset">
</Preference>
</PreferenceScreen>