Android,子类Preference类

发布于 2024-11-01 09:08:41 字数 1615 浏览 9 评论 0原文

我正在尝试编写 Android Honeycomb 应用程序,但在子类化 Preference 时遇到问题: http: //developer.android.com/reference/android/preference/Preference.html

我想制作一个带有标题和摘要的类似布局,还有一个进度条。

我已经创建了布局并添加了自定义首选项类,但我似乎无法获取它的实例来设置其中项目的值。

似乎首选项键不适用于自定义类。

这是我与标准首选项类相比的首选项定义:

               <Preference
                android:key="int_free_storage"
                android:title="Free Space"
                android:summary="free storage value here"/>

            <com.hamid.storageether.SpacePreference
                android:key="int_space_test"
                android:title="Test"
                android:summary="This is my custom preference"/>

然后,我的首选项子类将我的 XML 布局设置为它的构造函数中的布局资源

setLayoutResource(R.layout.space_pref_layout);

,它还覆盖 setTitle 和 setSummary 方法......

在我的主 PreferenceActivity 中,我尝试获取我的偏好是关键,但似乎不走运,因为偏好永远不会更新:

// These Two work
Preference intTotal = (Preference)findPreference("int_total_storage");
Preference intFree = (Preference)findPreference("int_free_storage");
           intTotal.setSummary("Standard Preference Summary 1");
           intFree.setSummary("Standard Preference Summary 2");

// My subclass doesn't - It just displays the default text defined in the layout xml.
SpacePreference intTest = (SpacePreference)findPreference("int_test_space");
            intTest.setTitle("Testtttyyy");
            intTest.setSummary("Test Summary");

有人可以指出我可能出错的地方吗?

I am tryig to write an Android Honeycomb application and I am having trouble subclassing Preference: http://developer.android.com/reference/android/preference/Preference.html

I want to make a similar layout with title and summary but also a progress bar.

I have created the layout and added the custom preference class but I can't seem to get hold of the instance of it to set the values of the items in it.

It seems that the preference key doesn't work for the custom class.

Here is my preference definition compared to the standard preference class:

               <Preference
                android:key="int_free_storage"
                android:title="Free Space"
                android:summary="free storage value here"/>

            <com.hamid.storageether.SpacePreference
                android:key="int_space_test"
                android:title="Test"
                android:summary="This is my custom preference"/>

My my preference subclass then sets my XML layout as it's layout resource in its constructor

setLayoutResource(R.layout.space_pref_layout);

it also overrides the setTitle and setSummary methods....

In my main PreferenceActivity I try to get hold of my Preference by it's key but no luck it seems, since the preference never gets updated:

// These Two work
Preference intTotal = (Preference)findPreference("int_total_storage");
Preference intFree = (Preference)findPreference("int_free_storage");
           intTotal.setSummary("Standard Preference Summary 1");
           intFree.setSummary("Standard Preference Summary 2");

// My subclass doesn't - It just displays the default text defined in the layout xml.
SpacePreference intTest = (SpacePreference)findPreference("int_test_space");
            intTest.setTitle("Testtttyyy");
            intTest.setSummary("Test Summary");

Could someone please point me towards where I may be going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

葮薆情 2024-11-08 09:08:41

该代码是直接从程序复制还是重新输入?如果复制,则您的密钥在 XML 中为“int_space_test”,在代码中为“int_test_space”。如果是这种情况,它应该在使用 intTest 的下一行抛出空指针异常。

Is this code copied straight from the program or retyped? If copied, then your key is "int_space_test" in XML and "int_test_space" in code. It should be throwing a null pointer exception on the next line where you use intTest if that's the case.

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