如何将所选数据显示到列表首选项摘要中
我是 Android 编程新手。在我的应用程序中,我添加了首选项类,并且我正在调用存储在我的应用程序的 xml 文件夹中的 xml 首选项。这是它的代码
在 java 类中我只写了一行,就是这样 addPreferencesFromResource(R.xml.prefs);
现在我可以显示首选项类并列出一些颜色数据。
我的问题是选择列表首选项,它应该向我显示摘要属性中选定的首选项。目前,它向我显示“可供选择的颜色列表” 我想要的是显示所选的颜色,假设我选择了白色,摘要应该显示为白色。
请帮我 。
谢谢你, 麦迪。
i am new to android programming .well in my application i have added the preference class and i am calling the xml preference which is stored in the xml folder of my application.this is the code for it
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference android:title="colors" android:key="colors"
android:summary="list of colors to choose from " android:entries="@array/list"
android:entryValues="@array/lvalues"></ListPreference>
</PreferenceScreen>
and in the java class i had written only one line that is thisaddPreferencesFromResource(R.xml.prefs);
now i am able to show the preference class and also the list some colors data.
my question is on selecting the list preference it should show me the selected preference in to the summary attribute .currently it is showing me as 'list of colors to choose from'
what i want is to show the selected color say i have selected white the summary should show me as white.
please help me .
Thank you,
maddy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上你可以用 xml 来做到这一点。试试这个:
您选择的值将替换“%s”。
如需进一步说明,请阅读这篇帖子
you can actually do it in xml. Try this:
your selected value would replace "%s".
For further clarification read this post
在另一个代码示例中,有一个小错误导致显示旧值而不是新值。
这是一个工作版本:
In the other code example there is a little bug which causes that the old value is displayed and not the new one.
Here is a working version:
您需要扩展 ListPreference 类并在 xml 文件中指定
如果您的自定义首选项类名称是 com.sample.MyListPref,则 xml 条目将为
You need to extend ListPreference class and specify that in the xml file
If your Custom Preference class name is com.sample.MyListPref the xml entry will be
您不需要扩展 listPreferences。在您的 PreferencesActivity 文件中按照我的示例放置:
更改时,值将更新标题
当您进入应用程序时,您需要使用您当前的首选项填充标题,而不是在 xml 中默认设置的颜色
You dont need to do the extend listPreferences. in your PreferencesActivity file put as per my example:
on change the value will update the title
when you enter the application you will need to populate the title with your current preference though instead of colors which is set at default in your xml