我正在开发一个安卓应用程序,我已经在我的设置页面中添加了几个SeekBarPreference元素作为滑块。它们有不同的最大值,我认为这是一个问题,你看不到你选择的值。所以我想知道在preferences活动中显示SeekBarPreference当前值的最佳方式是什么。现在,我认为也许seekBarPreference element的摘要是最好的方法,但我对其他建议持开放态度。
以下是设置活动的外观:

以下是我到目前为止尝试过的代码:
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
((SeekBarPreference) findPreference(R.id.seekRounds)).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final int progress = Integer.valueOf(String.valueOf(newValue));
preference.setSummary(String.format("Current value: %d", progress));
}
});
}目前我得到的错误是Cannot resolve symbol 'seekRounds'。
这是我的root_preferences.xml:
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory app:title="@string/main_settings_header">
<SeekBarPreference
app:key="seekRounds"
android:title="Number of rounds"
android:summary="This is the summary"
android:max="2"
android:progress="0"
android:theme="@style/Base.Widget.AppCompat.SeekBar"
/>
</PreferenceCategory>发布于 2019-05-21 05:29:33
找到答案了。只需在SeekBarPreference中添加app:showSeekBarValue="true"即可。
如下所示:
<SeekBarPreference
app:key="seekRounds"
android:title="Number of rounds"
android:summary="This is the summary"
android:max="2"
app:showSeekBarValue="true"
android:progress="0"
android:theme="@style/Base.Widget.AppCompat.SeekBar"
/>https://stackoverflow.com/questions/56211987
复制相似问题