从preferenceScreen去一个对话框preference [英] Going from a PreferenceScreen to a DialogPreference

查看:131
本文介绍了从preferenceScreen去一个对话框preference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序实际上是一个preferenceActivity的设置菜单。 当它的创建,如果一个布尔值未设置我想去的对话框preference其中规定了。

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:   无法找到明确的活动类   {com.xxxx / com.xxxx.xxxx preference};   有你在宣布本次活动   你的Andr​​oidManifest.xml?

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?

我应该怎么办呢?这是确定以添加对话框preference到清单?

How should I do this? It's ok to add that DialogPreference to the manifest?

推荐答案

A 对话preference 不是活动在自己的权利。这只是一个 preference 它显示一个对话框点击时。

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

现在的问题是,有没有明显的方式编程点击 preference 。但是,由于您使用的是对话preference 你已经得到了你的它自己的子类。

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();
}

然后在的onCreate()你的 preferencesActivity 你有这样的事情来加载从你的XML文件中的preferences:

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);

之后,你可以把一些code是这样的:

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.

另一种选择是,以取代对话框 prefenceActivity 这包含了所有你想要的选项保持和那么你可以通过意图,但我假设有你想要自己定制一个很好的理由对话框与特定的布局。如果你想要第二个 preferenceActivity 您可以按以下将其添加到您的preferences XML文件:

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>

这篇关于从preferenceScreen去一个对话框preference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆