1

我在实施首选项时遇到问题。当我运行我的应用程序并单击首选项选项卡时,没有任何反应。我不知道代码有什么问题,因为 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>
4

3 回答 3

3

将此行添加到您的代码中:

addPreferencesFromResource(R.xml.preferences);

此代码有效:

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class Reset extends PreferenceActivity{

private Preference myPreference;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
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;
    }
});

}

}

并记得添加您的活动:重置到清单:

于 2012-11-13T13:49:10.003 回答
1

您缺少的关键线是

addPreferencesFromResource(R.xml.preferences);

一旦你添加了一切都应该工作。

public class Reset extends PreferenceActivity {

    private Preference myPreference;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        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;
            }
        });

    }

}
于 2012-11-13T13:35:14.873 回答
0

使用 .equals 而不是 == 来比较对象。

if (preference.equals(myPrefernce)){

...

}
于 2012-11-13T12:38:25.600 回答