关于Android PreferenceActivity的一堆问题
我正在使用 PreferenceActivity
设置有关我拥有的另一个 BroadcastReciever
的一些首选项。
虽然使用 XML 文件制作首选项 GUI 的框架非常简单,但我无法找到如何使用这些首选项实际执行某些操作的手脚。特别是:
如何注册我的
BroadcastReciever
来监听我的偏好设置的变化? 看起来广播接收器的上下文与活动不同,因此我没有注册到正确的SharedPreferences
因为我不会拦截这些更改。当偏好设置发生变化时,我该如何在我的
PreferenceActivity
中采取行动?我想我不需要将PreferenceActivity
注册为首选项的侦听器,因此必须有更简单的方法。如何更改
PreferenceActivity
的 UI 以适应首选项的更改?例如,将首选项的“Summary”属性设置为用户选择的值?如何启用或禁用某些依赖于另一个首选项的首选项(例如
CheckBoxPreference
)?因此,用户只有先启用该功能才能编辑这些首选项。
我查看了所有文档,但除了从 XML 添加首选项之外,没有任何使用 PreferenceActivity
的示例。
我觉得我在这里错过了一些巨大的东西,因为它看起来很简单,但我无法弄清楚......
请尝试回答我的任何问题。
顺便提一句: 我正在针对 Android 1.6 进行开发,因此所有新的 PreferenceFragment
都无法使用。
I'm using PreferenceActivity
to set some preferences about another BroadcastReciever
that i have.
While making the skeleton of the preferences GUI is really simple with XML file, i cant find my hands and legs around how to actually DO something with those preferences. especially:
How do i register my
BroadcastReciever
to listen to changes in my preferences?
Its look like that the context of the broadcast reciever is not the same as the activity so i'm not registering to the rightSharedPreferences
because i dont intercept those changes.How do i act in my
PreferenceActivity
upon changes in the preferences? i guess i dont need to registerPreferenceActivity
as listener to the preferences, so there must be more simple way.How do i change the UI of the
PreferenceActivity
appropriate to the changes in the preferences? for example, set the "Summary" attribute of a preference to the value that the user chose?How do i enable or disable some preferences that depends on another preference (like
CheckBoxPreference
)? so the user could edit those preferences only if he enabled the feature first.
I looked al over the documentation but there is no example of using PreferenceActivity
beyond the point of just adding preferences from XML.
I feel like i'm missing something huge here, because it looks so simple, and yet i can't figure it out...
Please try to answer on any of my question.
BTW:
I'm developing for Android 1.6 so all the new PreferenceFragment
can't be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
BroadcastReceiver
已在清单中注册,您就不会监听首选项的更改,因为BroadcastReceiver
只会存在几毫秒。只需在onReceive()
期间读取最新值即可。如果
BroadcastReceiver
是由调用registerReceiver()
的其他组件注册的,则该组件可以使用SharedPreferences
注册OnSharedPreferenceChangeListener
> 通过PreferenceManager
和getDefaultSharedPreferences()
检索。这句话没有任何意义。
使用通过
PreferenceManager
和getDefaultSharedPreferences()
检索的SharedPreferences
注册一个OnSharedPreferenceChangeListener
。一般来说,你不会。欢迎您使用通过
PreferenceManager
和getDefaultSharedPreferences()
检索的SharedPreferences
注册OnSharedPreferenceChangeListener
。从那里,您可以通过findPreference()
从PreferenceActivity
获取Preference
并进行调整以适应。根据需要将
android:dependency
添加到您的首选项 XML。If the
BroadcastReceiver
is registered in the manifest, you don't listen to changes in your preferences, because theBroadcastReceiver
will only be around for milliseconds. Just read the latest values in duringonReceive()
.If the
BroadcastReceiver
is registered by some other component callingregisterReceiver()
, that component can register anOnSharedPreferenceChangeListener
with theSharedPreferences
retrieved viaPreferenceManager
andgetDefaultSharedPreferences()
.That sentence makes no sense.
Register an
OnSharedPreferenceChangeListener
with theSharedPreferences
retrieved viaPreferenceManager
andgetDefaultSharedPreferences()
.Generally, you don't. You are welcome to register an
OnSharedPreferenceChangeListener
with theSharedPreferences
retrieved viaPreferenceManager
andgetDefaultSharedPreferences()
. From there, you can get thePreference
from yourPreferenceActivity
viafindPreference()
and adjust to suit.Add
android:dependency
to your preference XML as needed.