AndroidX之前和之后DialogPreference之间的区别 [英] Difference between DialogPreference before and after AndroidX

查看:359
本文介绍了AndroidX之前和之后DialogPreference之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在通过我们的Android应用程序项目迁移到Androidx命名空间.但是我注意到,不仅名称空间似乎已更改.对于DialogPreference,现在也缺少以前使用的一些接口

We are currently migrating to Androidx namespace with our Android app project. However I noticed that not only the namespace seems to have changed. For DialogPreference also some interfaces which were using before are now missing

  • new interfaces: https://developer.android.com/reference/androidx/preference/DialogPreference
  • old interfaces: https://developer.android.com/reference/kotlin/android/preference/DialogPreference

例如,似乎缺少以下方法:onBindDialogView,showDialog,onDialogClosed.

For example the following methods seem to be missing: onBindDialogView, showDialog, onDialogClosed.

由于我们使用了其中一些方法来影响对话框的默认行为,因此我不清楚现在应如何实现此功能.例如,我们在关闭对话框之前验证输入,将值保存在数据库中而不是sharedpreferences中,并向对话框中添加一些动态元素.

Since we use some of these methods to influence the default behavior of the dialog, it is unclear to me how I should realize this functionality now. For example we are validating the input before closing the dialog, we are saving the value in a database instead of the sharedpreferences and adding some dynamic elements to the dialog.

其他人是否已经遇到此问题并找到了解决方案?我错过了文档中的任何内容吗?我们可以/应该使用另一个概念吗?

Has anyone else already encountered this problem and found a solution? Did I miss anything in the documentation? Is there another concept that we can / should use?

可以使用Fragments代替DialogPreference,但是对于少量内容(例如,用户可以从中选择的树项目列表)来说,这似乎给我带来了很多负担...

It would be possible to use Fragments instead of DialogPreference but for small amounts of content (e.g. a list of tree items, where the user can choose from) this seems to be a lot of overhead for me...

推荐答案

从androidx源文件开始,我已将基于旧 DialogPreference 的自定义类迁移到新的 androidx.preference.DialogPreference ,其操作步骤如下:

Starting from androidx source files, I've migrated custom classes based on old DialogPreference to new androidx.preference.DialogPreference with the following procedure:

基于旧DialogPreference的旧的自定义对话框类(例如CustomDialogPreference)应分为两个单独的类:

The old custom dialog class (e.g. CustomDialogPreference) based on legacy DialogPreference should be split into two separate classes:

  1. 一个类(例如CustomPreference)应扩展 androidx.preference.DialogPreference ,并且仅包含与首选项处理(数据管理)相关的代码.
  2. 另一个类(例如CustomDialog)应扩展 androidx.preference.PreferenceDialogFragmentCompat ,并且仅包含与对话框处理(用户界面)相关的代码,包括 onDialogClosed .此类应公开一个静态方法 newInstance 以返回此类的实例.
  1. One class (e.g. CustomPreference) should extend androidx.preference.DialogPreference and will contain only the code related to preference handling (data management).
  2. Another class (e.g. CustomDialog) should extend androidx.preference.PreferenceDialogFragmentCompat and will contain only the code related to dialog handling (user interface), including onDialogClosed. This class should expose a static method newInstance to return an instance of this class.

步骤2

在基于 PreferenceFragmentCompat 的主要片段处理首选项中,应覆盖 onDisplayPreferenceDialog 方法以显示自定义对话框,例如:

Step 2

In the main fragment handling preferences based on PreferenceFragmentCompat the onDisplayPreferenceDialog method should be overridden to show the custom dialog, e.g.:

    private static final String DIALOG_FRAGMENT_TAG = "CustomPreference";

    @Override
    public void onDisplayPreferenceDialog(Preference preference) {
        if (getParentFragmentManager().findFragmentByTag(DIALOG_FRAGMENT_TAG) != null) {
            return;
        }

        if (preference instanceof CustomPreference) {
            final DialogFragment f = CustomDialog.newInstance(preference.getKey());
            f.setTargetFragment(this, 0);
            f.show(getParentFragmentManager(), DIALOG_FRAGMENT_TAG);
        } else {
            super.onDisplayPreferenceDialog(preference);
        }
    }

这篇关于AndroidX之前和之后DialogPreference之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆