Android,子类Preference类
我正在尝试编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码是直接从程序复制还是重新输入?如果复制,则您的密钥在 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.