

<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" xmlns:example="http://schemas.android.com/apk/res/com.mnm.seekbarpreference"> <com.mnm.seekbarpreference.SeekBarPreference android:key="seekBarPreference" android:title="@string/dialog_title" android:dialogTitle="@string/dialog_title" android:summary="@string/summary" android:persistent="true" android:defaultValue="20" example:minValue="10" example:maxValue="50" /> </PreferenceScreen> <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="com.mnm.seekbarpreference.SeekBarPreference"> <attr name="minValue" format="integer" /> <attr name="maxValue" format="integer" /> </declare-styleable> </resources> mMinValue = attrs.getAttributeIntValue(PREFERENCE_NS, ATTR_MIN_VALUE, DEFAULT_MIN_VALUE); mMaxValue = attrs.getAttributeIntValue(PREFERENCE_NS, ATTR_MAX_VALUE, DEFAULT_MAX_VALUE); mDefaultValue = attrs.getAttributeIntValue(ANDROID_NS, ATTR_DEFAULT_VALUE, DEFAULT_CURRENT_VALUE); private static final String PREFERENCE_NS = "http://schemas.android.com/apk/res/com.mnm.seekbarpreference"; private static final String ANDROID_NS = "http://schemas.android.com/apk/res/android"; private static final String ATTR_DEFAULT_VALUE = "defaultValue"; private static final String ATTR_MIN_VALUE = "minValue"; private static final String ATTR_MAX_VALUE = "maxValue"; @Override protected View onCreateDialogView() { // mCurrentValue = getPersistedInt(mDefaultValue); // LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.dialog_slider, null); // ((TextView) view.findViewById(R.id.min_value)).setText(Integer.toString(mMinValue)); ((TextView) view.findViewById(R.id.max_value)).setText(Integer.toString(mMaxValue)); // SeekBar mSeekBar = (SeekBar) view.findViewById(R.id.seek_bar); mSeekBar.setMax(mMaxValue - mMinValue); mSeekBar.setProgress(mCurrentValue - mMinValue); mSeekBar.setOnSeekBarChangeListener(this); // mValueText = (TextView) view.findViewById(R.id.current_value); mValueText.setText(Integer.toString(mCurrentValue)); return view; } mSeekBar.setOnSeekBarChangeListener(this) . It is necessary to implement only one method from three possible: public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) { mCurrentValue = value + mMinValue; mValueText.setText(Integer.toString(mCurrentValue)); } onDialogClosed changes, the onDialogClosed method is onDialogClosed , which you need to override: @Override protected void onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); if (!positiveResult) { return; } if (shouldPersist()) { persistInt(mCurrentValue); } notifyChanged(); } shouldPersist() analyzes whether it should be done. This checks the android:persistent flag specified in preferences.xml . @Override public CharSequence getSummary() { String summary = super.getSummary().toString(); int value = getPersistedInt(mDefaultValue); return String.format(summary, value); } notifyChanged() .Source: https://habr.com/ru/post/114733/
All Articles