编程滚动preferenceActivity [英] programmatically scroll PreferenceActivity

查看:252
本文介绍了编程滚动preferenceActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

preferenceActivity 用大量 preferenceCategories 在它定义的。如果我有的android:给定类别的关键

I have PreferenceActivity with plenty of PreferenceCategories defined in it. If I have the android:key of a given category.

是否有可能以编程方式滚动活动到这一类?

Is it possible programmatically to scroll the Activity to this category?

推荐答案

您可以通过preferences在重复这样的活动:

You can iterate through the preferences in the activity like this:

PreferenceScreen screen = getPreferenceScreen();
int i;
for(i = 0; i < screen.getPreferenceCount(); i++) {
   String key = screen.getPreference(i).getKey();

   // be careful, because key will be null if no android:key is specified
   // (as is often the case for PreferenceCategory elements)
   if("myKey".equals(key))
      break;
}

// PreferenceActivity extends ListActivity, so the ListView is accessible...
getListView().setSelection(i);

测试了与Android SDK 14和它工作正常。

Tested out with Android SDK 14 and it works fine.

注意,虽然的,叫 getListView()。setSelection(I)里面的onCreate或者onResume没有任何效果。它具有活动绘制之后被调用。

Caution though, calling getListView().setSelection(i) inside onCreate or onResume has no effect. It has to be called after the activity is drawn.

的get preferenceCount()方法计算所有preferenceCategories和嵌套preferences。不知道它确实为preferenceScreens,但我敢肯定,一个小实验会有启发。

The getPreferenceCount() method counts all PreferenceCategories and their nested preferences. Not sure what it does for PreferenceScreens, although I'm sure a little experimentation there would be revealing.

这篇关于编程滚动preferenceActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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