从PreferenceScreen转到DialogPreference[英] Going from a PreferenceScreen to a DialogPreference

本文是小编为大家收集整理的关于从PreferenceScreen转到DialogPreference的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我的应用程序有一个设置菜单,它实际上是offormenceActivity. 当它创建时,如果未设置布尔值,我想转到对象,该方法设置为此.

我尝试用意图做它,但申请力关闭了这个错误msg:

E/AndroidRuntime(239): Android.Content.ActivityNotFoundException: 无法找到显式活动类 {com.xxxx/com.xxxx.xxxxpreference}; 你有没有宣布过这项活动 您的androidmanifest.xml?

我该怎么办?可以将Dateverference添加到清单?

推荐答案

a DialogPreference在其自己的右侧不是Activity.它只是一个Preference,它在单击时显示Dialog.

问题是,没有显着的方式以编程方式单击A Preference.但是,由于您使用的是DialogPreference,您已经让您拥有自己的子类.因此,我们可以通过将以下方法添加到您的DialogPreference:

的子类来解决我们的问题
//Expose the protected onClick method
void show() {
    onClick();
}
然后在PreferencesActivity中的onCreate()中,您将有类似的东西来从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方法是理想的,但它确实工作了.

另一个选项是用PrefenceActivity替换Dialog,其中包含您希望维护的所有选项,然后您可以通过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>

其他推荐答案

要使用意图启动活动,活动必须在Android清单中.只需添加一条线:

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

本文地址:https://www.itbaoku.cn/post/102505.html

问题描述

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?

推荐答案

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>

其他推荐答案

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"/>