onPreferenceClick在android中不起作用

时间:2012-11-13 12:33:50

标签: android

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

3 个答案:

答案 0 :(得分: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;
    }
});

}

}

请记住添加您的活动:重置为清单:           

答案 1 :(得分: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;
            }
        });

    }

}

答案 2 :(得分:0)

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

if (preference.equals(myPrefernce)){

...

}