onpreferenceclicklistener

onpreferenceclicklistener

在我的应用程序中,我希望在应用程序中使用某个首选项时显示警告。我试过使用preferenceactivity.onpreferencetreeclick和onpreferenceclicklistener,但两者都不起作用。下面是后一个例子。有人能解释一下吗?

@Override
protected void onCreate(Bundle saved) {

    super.onCreate(saved);
    addPreferencesFromResource(R.xml.prefs);
    Preference gridview = findPreference("fcngrid");
    gridview.setOnPreferenceClickListener(overrider);

}

public OnPreferenceClickListener overrider = new OnPreferenceClickListener() {

    public boolean onPreferenceClick(Preference pref) {

        if (pref.getTitle() == "Default as grid")
            showDialog(0);

        return false;
    }

};

protected Dialog onCreateDialog(int id) {

    AlertDialog.Builder b = new AlertDialog.Builder(this);
    b.setTitle("Warning")
     .setMessage("Unfortunately, this feature is currently unstable, and will be" +
             " fixed soon.")
     .setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            dialog.cancel();

        }

     });

    return b.create();

}

最佳答案

别担心,使用pref.getTitle().equals("Default as grid")效果很好。

08-17 15:45