Android:在首选项 xml 中多次使用相同的首选项
我有一个 EditTextPreference,我想在两个不同的 PreferenceScreen 中向用户显示。它应该是完全相同的首选项(让 android:key 为“myEditText”),只是显示在两个不同的屏幕上。 所以,它可能看起来像这样(完全精简以向您展示一个示例):
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/someCategory">
<PreferenceScreen android:key="someScreen">
<PreferenceScreen android:key="someSubScreen">
<PreferenceCategory android:title="@string/someSubCategory">
<EditTextPreference android:key="myEditText"
....
/>
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="@string/someOtherCategory">
<PreferenceScreen android:key="someOtherScreen">
<PreferenceScreen android:key="someOtherSubScreen">
<PreferenceCategory android:title="@string/someOtherSubCategory">
<EditTextPreference android:key="myEditText"
....
/>
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
当我通过 someScreen 转到 myEditText 并输入文本时,当我返回并通过 someOtherScreen 打开 myEditText 时,它不会显示。我必须关闭首选项并再次打开它。然后,两个 EditText 显示相同的文本。
那么,在不同屏幕上拥有完全相同的首选项的最佳方法是什么?
/编辑:
无论如何,我为什么要这样做? 第一个 PreferenceScreen 是关于 URL 缩短的。您可以设置缩短选项,选择/重新排序您想要使用的服务,并输入 bit.ly、goo.gl 等的用户凭据。
第二个屏幕是文件上传服务,您可以在其中执行相同的操作(设置文件上传选项、选择/重新排序服务、输入上传服务的用户凭据)。
现在我正在集成 CloudApp,它既是文件又是 URL 缩短服务。因此,用户可能希望在 URL 缩短屏幕上找到它,但他也可能在文件上传屏幕上查找它。因此,我想让他轻松地在两个屏幕上显示它。它们不是顶级屏幕,而是相当深层的结构。
那么,除了这是否有意义之外:这可能吗?
I have an EditTextPreference that I want to show to the user in two different PreferenceScreens. It is supposed to be the exact same preference (let the android:key be "myEditText"), just shown on two different screens.
So, here's what it could look like (totally stripped down to show you an example):
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/someCategory">
<PreferenceScreen android:key="someScreen">
<PreferenceScreen android:key="someSubScreen">
<PreferenceCategory android:title="@string/someSubCategory">
<EditTextPreference android:key="myEditText"
....
/>
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="@string/someOtherCategory">
<PreferenceScreen android:key="someOtherScreen">
<PreferenceScreen android:key="someOtherSubScreen">
<PreferenceCategory android:title="@string/someOtherSubCategory">
<EditTextPreference android:key="myEditText"
....
/>
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
When I go to myEditText via someScreen and enter a text, it is not shown when I go back and open myEditText via someOtherScreen. I have to close the preferences and open it again. Then, both EditTexts show the same text.
So, what would be the best way to have the exact same Preference on different screens?
/Edit:
Why do I want to do that, anyway?
The first PreferenceScreen is all about URL shortening. You can set options for shortening, select / reorder services you want to use and enter user credentials for bit.ly, goo.gl etc.
The second screen is for file upload services where you can do the same (setting options for file upload, selecting / reordering services, entering user credentials for upload services).
Now I'm integrating CloudApp which is both, a file AND a URL shortening service. So, the user might be epecting to find it on the URL shortening screen but he might also look for it at the file upload screen. So, I want to make it easy for him and just show it on both screens. They are not top-level screens, it's quite a deep structure.
So, besides this making sense or not: Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这就是我现在正在做的事情。它并不漂亮,但它完成了工作,目前我对此感到满意:
不要使用相同的键,而是使用不同的 EditTextPreferences(例如 myEditText 和 myEditText2)。
在首选项代码中创建一个 OnPreferenceChangeListener:
将侦听器分配给两个 EditTextPreferences:
在注册 OnSharedPreferenceChangeListener 的主活动中,我只需要监视对第一个 EditText 的更改。事实上,我完全忽略了第二个 EditText,因为它始终具有与第一个 EditText 相同的条目。
Okay, here's what I'm doing now. It's not pretty, but it does the job and at the moment I'm content with that:
Instead of using the same key, use different EditTextPreferences (say myEditText and myEditText2).
In the preference code create an OnPreferenceChangeListener:
Assign the Listener to both EditTextPreferences:
In the main activity where the OnSharedPreferenceChangeListener is registered, I only need to monitor changes to the first EditText. In fact, I totally ignore the second EditText because it always has the same entry as the first EditText.