覆盖android偏好活动中的点击

发布于 2024-09-18 08:49:30 字数 1124 浏览 6 评论 0原文

在我的应用程序中,我想在应用程序中使用特定首选项时显示警告。我尝试过使用 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();

}

In my app I'd like to show a warning when using a certain preference in my application. I've tried using the PreferenceActivity.onPreferenceTreeClick and the onPreferenceClickListener, but neither seems to work. I've got the latter example below. Could anyone shed some light on this please?

@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();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2024-09-25 08:49:30

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

Don't worry, using pref.getTitle().equals("Default as grid") worked fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文