问题描述
我们目前正在使用我们的 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
- 新接口:https://developer.android.com/reference/androidx/preference/DialogPreference
- 旧接口:https://developer.android.com/reference/kotlin/android/preference/DialogPreference
例如,似乎缺少以下方法:onBindDialogView、showDialog、onDialogClosed.
For example the following methods seem to be missing: onBindDialogView, showDialog, onDialogClosed.
由于我们使用其中一些方法来影响对话框的默认行为,我不清楚现在应该如何实现此功能.例如,我们在关闭对话框之前验证输入,我们将值保存在数据库中而不是共享首选项中,并向对话框添加一些动态元素.
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:
- 一个类(例如 CustomPreference)应该扩展 androidx.preference.DialogPreference 并且只包含与首选项处理(数据管理)相关的代码.
- 另一个类(例如 CustomDialog)应该扩展 androidx.preference.PreferenceDialogFragmentCompat 并且只包含与对话框处理(用户界面)相关的代码,包括 onDialogClosed.此类应公开一个静态方法 newInstance 以返回此类的实例.
- One class (e.g. CustomPreference) should extend androidx.preference.DialogPreference and will contain only the code related to preference handling (data management).
- 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 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!