从 PreferenceScreen 到 DialogPreference
我的应用程序有一个设置菜单,它实际上是一个 PreferenceActivity。 创建时,如果未设置布尔值,我想转到设置该值的 DialogPreference。
我尝试有意这样做,但应用程序强制关闭并显示以下错误消息:
E/Android运行时(239): android.content.ActivityNotFoundException: 无法找到明确的活动类别 {com.xxxx/com.xxxx.xxxx首选项}; 您是否已在以下日期宣布此活动: 你的AndroidManifest.xml?
我该怎么做?将 DialogPreference 添加到清单中可以吗?
My application has a setting menu which is actually a PreferenceActivity.
When it's created, if a boolean value is not set I want to go to the DialogPreference which sets that.
I tried doing it with an intent but the application force closed with this error msg:
E/AndroidRuntime( 239):
android.content.ActivityNotFoundException:
Unable to find explicit activity class
{com.xxxx/com.xxxx.xxxxPreference};
have you declared this activity in
your AndroidManifest.xml?
How should I do this? It's ok to add that DialogPreference to the manifest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DialogPreference
本身并不是一个Activity
。它只是一个Preference
,单击时会显示一个Dialog
。问题是没有明显的方法以编程方式单击
首选项
。但是,由于您使用的是DialogPreference
,您已经拥有了它自己的子类。因此,我们可以通过将以下方法添加到DialogPreference
的子类中来解决我们的问题:然后在
PreferencesActivity
的onCreate()
中,您将有这样的东西从你的 XML 文件加载首选项:之后你可以放置一些像这样的代码:
这是一个有点黑客,因为公开
protected
方法并不理想,但它确实有效。另一种选择是将
Dialog
替换为PrefenceActivity
,其中包含您希望维护的所有选项,然后您可以通过Intent
启动它,但我假设您有充分的理由希望您自己的自定义Dialog
具有特定的布局。如果您确实需要第二个PreferenceActivity
,您可以将其添加到您的首选项 XML 文件中,如下所示:A
DialogPreference
isn't anActivity
in its own right. It's just aPreference
which displays aDialog
when clicked.The problem is that there's no obvious way programmatically click a
Preference
. However, since you're usingDialogPreference
you've already got you own subclass of it. So we can solve our problem by adding the following method to your subclass ofDialogPreference
:Then in the
onCreate()
of yourPreferencesActivity
you'll have something like this to load the preferences from your XML file:After that you can put some code like this:
This is a bit of hack, as exposing
protected
methods isn't ideal, but it does work.Another option would be to replace the
Dialog
with aPrefenceActivity
which contained all the options you wish to maintain and then you could launch it via anIntent
, but I'm assuming there's a good reason that you want your own customDialog
with a specific layout. If you do want a secondPreferenceActivity
you can add it to your preferences XML file as follows:要使用 Intent 启动 Activity,该 Activity 必须位于 Android 清单中。只需添加一行:
To start an activity with an Intent, the activity must be in the Android manifest. Just add a line like: