如何以编程方式获取在 XML 中定义的 ListPreference
的默认值?
这是我的 ListPreference
的 fragment :
<ListPreference
android:defaultValue="60"
android:entries="@array/interval_entries"
android:entryValues="@array/interval_values"
android:key="interval"
android:summary="@string/interval_summary"
android:title="@string/interval_title" />
我已经阅读了文档,但我还没有找到获取它的方法。也许我忽略了它。
最佳答案
对于 PreferenceActivity(已弃用 Fragment),请尝试:
ListPreference lp = (ListPreference) this.findPreference(this.getString(R.string.my_key));
lp.getValue();
其中 my_key 是分配给此 ListPreference 的键值。注意:此值在 strings.xml
中定义。如果您已使用文字字符串对 key 进行硬编码,则将 my_key 替换为您为 android:key
标记提供的任何字符串。因此,在您的情况下,代码将是:
ListPreference lp = (ListPreference) this.findPreference("interval");
lp.getValue();
关于java - 如何获取 XML 中定义的 ListPreference 的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21044126/