从 PreferenceScreen 到 DialogPreference

发布于 2024-08-19 19:33:25 字数 360 浏览 5 评论 0原文

我的应用程序有一个设置菜单,它实际上是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走过海棠暮 2024-08-26 19:33:25

DialogPreference 本身并不是一个Activity。它只是一个Preference,单击时会显示一个Dialog

问题是没有明显的方法以编程方式单击首选项。但是,由于您使用的是 DialogPreference,您已经拥有了它自己的子类。因此,我们可以通过将以下方法添加到 DialogPreference 的子类中来解决我们的问题:

//Expose the protected onClick method
void show() {
    onClick();
}

然后在 PreferencesActivityonCreate() 中,您将有这样的东西从你的 XML 文件加载首选项:

// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);

之后你可以放置一些像这样的代码:

booleanProp = true; //set this to the value of the property you're checking     

if (! booleanProp) {
    //Find the Preference via its android:key
    //MyDialogPreference is your subclasss of DialogPreference
    MyDialogPreference dp = (MyDialogPreference)getPreferenceScreen().findPreference("dialog_preference");  
    dp.show();
}

这是一个有点黑客,因为公开 protected 方法并不理想,但它确实有效。

另一种选择是将 Dialog 替换为 PrefenceActivity,其中包含您希望维护的所有选项,然后您可以通过 Intent 启动它,但我假设您有充分的理由希望您自己的自定义 Dialog 具有特定的布局。如果您确实需要第二个 PreferenceActivity,您可以将其添加到您的首选项 XML 文件中,如下所示:

<PreferenceScreen
        android:title="@string/title_of_preference"
        android:summary="@string/summary_of_preference">
    <intent android:action="your.action.goes.HERE"/>
</PreferenceScreen>

A DialogPreference isn't an Activity in its own right. It's just a Preference which displays a Dialog when clicked.

The problem is that there's no obvious way programmatically click a Preference. However, since you're using DialogPreference you've already got you own subclass of it. So we can solve our problem by adding the following method to your subclass of DialogPreference:

//Expose the protected onClick method
void show() {
    onClick();
}

Then in the onCreate() of your PreferencesActivity you'll have something like this to load the preferences from your XML file:

// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);

After that you can put some code like this:

booleanProp = true; //set this to the value of the property you're checking     

if (! booleanProp) {
    //Find the Preference via its android:key
    //MyDialogPreference is your subclasss of DialogPreference
    MyDialogPreference dp = (MyDialogPreference)getPreferenceScreen().findPreference("dialog_preference");  
    dp.show();
}

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 a PrefenceActivity which contained all the options you wish to maintain and then you could launch it via an Intent, but I'm assuming there's a good reason that you want your own custom Dialog with a specific layout. If you do want a second PreferenceActivity you can add it to your preferences XML file as follows:

<PreferenceScreen
        android:title="@string/title_of_preference"
        android:summary="@string/summary_of_preference">
    <intent android:action="your.action.goes.HERE"/>
</PreferenceScreen>
萤火眠眠 2024-08-26 19:33:25

要使用 Intent 启动 Activity,该 Activity 必须位于 Android 清单中。只需添加一行:

<activity android:name=".path.to.MyActivity"/>

To start an activity with an Intent, the activity must be in the Android manifest. Just add a line like:

<activity android:name=".path.to.MyActivity"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文