如何打开或模拟在Android preference点击,使用XML创建,编程? [英] How to open or simulate a click on an android Preference, created with XML, programatically?

查看:310
本文介绍了如何打开或模拟在Android preference点击,使用XML创建,编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序与XML声明preferences,满载添加preferencesFromResource 。用户可以打开preferences,点击每个项目,并对其进行编辑,所有的作品。

I've an android application with preferences declared in XML, loaded with addPreferencesFromResource. The user can open preferences, click on each item and edit them, all works.

一preference我拥有的是:

One preference I have is:

        <ListPreference android:key="abc"
            android:title="@string/abc"
            android:summary="@string/cde"
            android:persistent="true"/>

我如何能自动显示出preference对话框给用户(而不需要用户去到preference屏幕,点击它?)。

How can I show the preference dialog to a user automatically (without the need for the user to go to the preference screen and click on it?).

我试过((机器人。preference.Dialog preference)prefMgr.find preference(ABC))。的ShowDialog(空),而是说,这是一个受保护的方法...?从我的主要活动(这是一个 preferenceActivity ),这就是为什么它显然不能正常工作的说法。但是,怎么回事?

I tried ( (android.preference.DialogPreference) prefMgr.findPreference( "abc" )).showDialog(null), but is says it is a protected method...? Called it from my main activity (which is a PreferenceActivity), that's why it obviously cannot work. But how else?

修改

我刚刚发现两个线程(<一href="https://groups.google.com/group/android-developers/browse_thread/thread/3b38965a44525190">1,和<一href="https://groups.google.com/group/android-developers/browse_thread/thread/6ee893906962e821">2)随着主意,用 findViewById 访问preference,但没有成功,它总是返回的的(确实对我来说,太)

I just found two threads (1, and 2) with the idea to use findViewById to access the preference, but with no success. It always returns null (does for me, too).

看起来真的是有没有可能从code做到这一点。

It looks like there is really no possibility to do this from code.

推荐答案

您可能扩展列表preference创建对话框,然后包括的话费清单preference保护ShowDialog方法你自己的公共方法。是这样的:

You could have extended ListPreference to create your dialog, then included your own public method that calls the protected showDialog method of ListPreference. Something like:

public void show()
{
    showDialog(null);
}

这样你就不会遇到getOrder(的问题),不工作的时候有preferenceGroups几个人已经指出,在评论你的答案。

This way you won't run into the issue of getOrder() not working when there are PreferenceGroups as several people have pointed out in the comments your answer.

这可以与具有保护ShowDialog方法的任何preference类型来完成。

This can be done with any preference types that has a protected showDialog method.

这篇关于如何打开或模拟在Android preference点击,使用XML创建,编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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