在PreferenceFragment中实现SeekBarPreference

时间:2017-09-24 07:31:04

标签: android android-layout android-fragments android-preferences android-seekbar

所以我必须在PreferenceFragment中实现SeekBarPreference。我正在使用android.support.v7.preference库提供的SeekBarPreference。但是,我不明白如何在PreferenceFragment中实现SeekBarPreference。

这是我的PreferenceActivity的代码:

package com.example.android.seismofeed;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.SeekBarPreference;

/**
 * Created by chaitanya on 28/03/17.
 */

public class SettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_activity);

}

public static class EarthquakePreferenceFragment extends PreferenceFragment
        implements Preference.OnPreferenceChangeListener {

    private SeekBarPreference mSeekBarPreference;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings_main);

        Preference minMagnitude = findPreference(getString(R.string.min_mag_key));
        bindPreferenceSummaryToValue(minMagnitude);

        Preference orderBy = findPreference(getString(R.string.order_by_key));
        bindPreferenceSummaryToValue(orderBy);

        Preference maxLimit = findPreference(getString(R.string.maxLimitKey));
        bindPreferenceSummaryToValue(maxLimit);

        Preference startDate = findPreference(getString(R.string.startDateKey));
        bindPreferenceSummaryToValue(startDate);

        Preference endDate = findPreference(getString(R.string.endDateKey));
        bindPreferenceSummaryToValue(endDate);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();

        if (preference instanceof ListPreference) {
            ListPreference listPreference = (ListPreference) preference;
            int indexOf = listPreference.findIndexOfValue(stringValue);
            if (indexOf >= 0) {
                CharSequence[] labels = listPreference.getEntries();
                preference.setSummary(labels[indexOf]);
            }
        }
        else
            preference.setSummary(stringValue);
        return true;
    }

    private void bindPreferenceSummaryToValue(Preference preference) {
        preference.setOnPreferenceChangeListener(this);
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(preference.getContext());
        String preferencesString = sharedPreferences.getString(preference.getKey(), "");
        onPreferenceChange(preference, preferencesString);
    }

}
}

我的偏好活动的xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title = "@string/settings_title">

    <ListPreference
        android:defaultValue="@string/order_by_default"
        android:entries="@array/order_label"
        android:entryValues="@array/order_value"
        android:key = "@string/order_by_key"
        android:title = "@string/order_by_label" />

    <EditTextPreference
        android:title = "@string/min_mag"
        android:key = "@string/min_mag_key"
        android:inputType="numberDecimal"
        android:selectAllOnFocus="true"
        android:defaultValue = "@string/min_mag_value" />

    <EditTextPreference
        android:title = "@string/maxLimitLabel"
        android:key = "@string/maxLimitKey"
        android:inputType="number"
        android:selectAllOnFocus="true"
        android:defaultValue="@string/maxLimitValue" />

    <EditTextPreference
        android:title = "@string/startDateLabel"
        android:key = "@string/startDateKey"
        android:inputType="date"
        android:selectAllOnFocus="true"
        android:defaultValue="@string/startDateValue" />

    <EditTextPreference
        android:title = "@string/endDateLabel"
        android:key = "@string/endDateKey"
        android:inputType="date"
        android:selectAllOnFocus="true"
        android:defaultValue="@string/endDateValue" />

    <SeekBarPreference
        android:key = "seekBar"
        android:summary = "Adjust magnitude"
        android:defaultValue = "25"
        android:min = "0"
        android:max = "50" />

</PreferenceScreen>

我已经想了很多但却无法想出实现SeekBarPreference的方法。我可以在Android Studio的预览中看到SeekBar,但是当我在模拟器上运行应用程序时,我的活动中没有SeekBar。

谢谢!

0 个答案:

没有答案