我的PreferenceScreen上的滑块

时间:2009-12-29 11:35:19

标签: android

我希望我的偏好菜单能够改变振动的持续时间。

prefs.xml没有slider标签,那么最好的方法是什么?

8 个答案:

答案 0 :(得分:40)

我改进了Macarse提供的链接,因此仅在单击ok按钮时保存该值,以便您可以在XML文件中使用@string/...值。

以下是代码:

/* The following code was written by Matthew Wiggins 
 * and is released under the APACHE 2.0 license 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Improvements :
 * - save the value on positive button click, not on seekbar change
 * - handle @string/... values in xml file
 */

package fr.atcm.carpooling.views.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;


public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener, OnClickListener
{
    // ------------------------------------------------------------------------------------------
    // Private attributes :
    private static final String androidns="http://schemas.android.com/apk/res/android";

    private SeekBar mSeekBar;
    private TextView mSplashText,mValueText;
    private Context mContext;

    private String mDialogMessage, mSuffix;
    private int mDefault, mMax, mValue = 0;
    // ------------------------------------------------------------------------------------------



    // ------------------------------------------------------------------------------------------
    // Constructor :
    public SeekBarPreference(Context context, AttributeSet attrs) {

        super(context,attrs); 
        mContext = context;

        // Get string value for dialogMessage :
        int mDialogMessageId = attrs.getAttributeResourceValue(androidns, "dialogMessage", 0);
        if(mDialogMessageId == 0) mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage");
        else mDialogMessage = mContext.getString(mDialogMessageId);

        // Get string value for suffix (text attribute in xml file) :
        int mSuffixId = attrs.getAttributeResourceValue(androidns, "text", 0);
        if(mSuffixId == 0) mSuffix = attrs.getAttributeValue(androidns, "text");
        else mSuffix = mContext.getString(mSuffixId);

        // Get default and max seekbar values :
        mDefault = attrs.getAttributeIntValue(androidns, "defaultValue", 0);
        mMax = attrs.getAttributeIntValue(androidns, "max", 100);
    }
    // ------------------------------------------------------------------------------------------



    // ------------------------------------------------------------------------------------------
    // DialogPreference methods :
    @Override 
    protected View onCreateDialogView() {

        LinearLayout.LayoutParams params;
        LinearLayout layout = new LinearLayout(mContext);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setPadding(6,6,6,6);

        mSplashText = new TextView(mContext);
        mSplashText.setPadding(30, 10, 30, 10);
        if (mDialogMessage != null)
            mSplashText.setText(mDialogMessage);
        layout.addView(mSplashText);

        mValueText = new TextView(mContext);
        mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
        mValueText.setTextSize(32);
        params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT, 
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.addView(mValueText, params);

        mSeekBar = new SeekBar(mContext);
        mSeekBar.setOnSeekBarChangeListener(this);
        layout.addView(mSeekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        if (shouldPersist())
            mValue = getPersistedInt(mDefault);

        mSeekBar.setMax(mMax);
        mSeekBar.setProgress(mValue);

        return layout;
    }

    @Override 
    protected void onBindDialogView(View v) {
        super.onBindDialogView(v);
        mSeekBar.setMax(mMax);
        mSeekBar.setProgress(mValue);
    }

    @Override
    protected void onSetInitialValue(boolean restore, Object defaultValue)  
    {
        super.onSetInitialValue(restore, defaultValue);
        if (restore) 
            mValue = shouldPersist() ? getPersistedInt(mDefault) : 0;
            else 
                mValue = (Integer)defaultValue;
    }
    // ------------------------------------------------------------------------------------------



    // ------------------------------------------------------------------------------------------
    // OnSeekBarChangeListener methods :
    @Override
    public void onProgressChanged(SeekBar seek, int value, boolean fromTouch)
    {
        String t = String.valueOf(value);
        mValueText.setText(mSuffix == null ? t : t.concat(" " + mSuffix));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seek) {}
    @Override
    public void onStopTrackingTouch(SeekBar seek) {}

    public void setMax(int max) { mMax = max; }
    public int getMax() { return mMax; }

    public void setProgress(int progress) { 
        mValue = progress;
        if (mSeekBar != null)
            mSeekBar.setProgress(progress); 
    }
    public int getProgress() { return mValue; }
    // ------------------------------------------------------------------------------------------



    // ------------------------------------------------------------------------------------------
    // Set the positive button listener and onClick action : 
    @Override
    public void showDialog(Bundle state) {

        super.showDialog(state);

        Button positiveButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
        positiveButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        if (shouldPersist()) {

            mValue = mSeekBar.getProgress();
            persistInt(mSeekBar.getProgress());
            callChangeListener(Integer.valueOf(mSeekBar.getProgress()));
        }

        ((AlertDialog) getDialog()).dismiss();
    }
    // ------------------------------------------------------------------------------------------
}

编辑:

以下是截图:

Screenshot SeekBarPreference

编辑:在arlomedia的要求下,这里是所有需要的代码片段(我刚刚重新创建了一个新的项目,它完全正常工作。我纠正了SeekBarPreference类中的一些错误,所以不要忘记重新复制/粘贴它):

MainActivity:

package fr.at.testsliderpref;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        switch(item.getItemId()) {

            case R.id.menu_settings : {

                startActivity(new Intent(this, SettingsActivity.class));

                break;
            }
        }

        return true;    
    }
}

SettingsActivity:

package fr.at.testsliderpref;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import fr.at.testsliderpref.utils.SeekBarPreference;

public class SettingsActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // Call super :
        super.onCreate(savedInstanceState);

        // Set the activity's fragment :
        getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
    }


    public static class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

        private SeekBarPreference _seekBarPref;

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.activity_settings);


            // Get widgets :
            _seekBarPref = (SeekBarPreference) this.findPreference("SEEKBAR_VALUE");

            // Set listener :
            getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

            // Set seekbar summary :
            int radius = PreferenceManager.getDefaultSharedPreferences(this.getActivity()).getInt("SEEKBAR_VALUE", 50);
            _seekBarPref.setSummary(this.getString(R.string.settings_summary).replace("$1", ""+radius));
        }

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

            // Set seekbar summary :
            int radius = PreferenceManager.getDefaultSharedPreferences(this.getActivity()).getInt("SEEKBAR_VALUE", 50);
            _seekBarPref.setSummary(this.getString(R.string.settings_summary).replace("$1", ""+radius));
        }
    }
}

布局> activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textview_text" />

</RelativeLayout>

菜单&gt; main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_settings"
        android:title="@string/menu_settings"
        android:icon="@android:drawable/ic_menu_preferences"/>

</menu>

xml&gt; activity_settings.xml:

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

        <fr.at.testsliderpref.utils.SeekBarPreference
            android:defaultValue="50"
            android:dialogMessage="@string/settings_dialog_message"
            android:key="SEEKBAR_VALUE"
            android:max="100"
            android:summary="@string/settings_summary"
            android:text="@string/settings_unit"
            android:title="@string/settings_title" />

</PreferenceScreen>

值&gt; strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">TestSliderPref</string>
    <string name="textview_text">SeekBarPreference test</string>

    <string name="menu_settings">Settings</string>
    <string name="settings_dialog_message">Here comes a message</string>
    <string name="settings_summary">Current value is $1</string>
    <string name="settings_unit">Km</string>
    <string name="settings_title">Here comes the title</string>

</resources>

不要忘记将您的SettingsActivity添加到清单中,它应该没问题。

答案 1 :(得分:19)

您可以创建自己的Preference类,扩展DialogPreference并在对话框视图中显示SeekBar

答案 2 :(得分:16)

实现此目的的一种简单方法是向您的preferences.xml添加一个空的首选项,该首选项使用包含搜索条的布局。

在您的preferences.xml中添加

<Preference android:layout="@layout/sliderlayout" />
布局下的

添加了sliderlayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

答案 3 :(得分:7)

这是一个很好的Slider Preference组件 - Android Slider Preference Library enter image description here

https://github.com/jayschwa/AndroidSliderPreference

答案 4 :(得分:2)

另外一个实现。 所有信用都归Tim Selfin所有。

我希望显示从XML Arrays

加载的值

看起来像这样

Seekbar setting loading items from XML

代码 - 现在扩展了ListPreference

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class SeekBarListPreference extends ListPreference implements SeekBar.OnSeekBarChangeListener, View.OnClickListener {
    // ------------------------------------------------------------------------------------------
    // Private attributes :
    private static final String androidns = "http://schemas.android.com/apk/res/android";

    private SeekBar mSeekBar;
    private TextView mSplashText, mValueText;
    private Context mContext;

    private String mDialogMessage;
    // ------------------------------------------------------------------------------------------


    public SeekBarListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        mContext = context;

        // Get string value for dialogMessage :
        int mDialogMessageId = attrs.getAttributeResourceValue(androidns, "dialogMessage", 0);
        if (mDialogMessageId == 0)
            mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage");
        else mDialogMessage = mContext.getString(mDialogMessageId);
    }

    // ------------------------------------------------------------------------------------------
    // DialogPreference methods :
    @Override
    protected View onCreateDialogView() {

        LinearLayout.LayoutParams params;
        LinearLayout layout = new LinearLayout(mContext);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setPadding(6, 6, 6, 6);

        mSplashText = new TextView(mContext);
        mSplashText.setPadding(30, 10, 30, 10);
        if (mDialogMessage != null)
            mSplashText.setText(mDialogMessage);
        layout.addView(mSplashText);

        mValueText = new TextView(mContext);
        mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
        mValueText.setTextSize(32);
        params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.addView(mValueText, params);

        mSeekBar = new SeekBar(mContext);
        mSeekBar.setOnSeekBarChangeListener(this);
        layout.addView(mSeekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        setProgressBarValue();

        return layout;
    }

    @Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
        // do not call super
    }

    private void setProgressBarValue() {
        String mValue = null;
        if (shouldPersist()) {
            mValue = getValue();
        }

        final int max = this.getEntries().length - 1;

        mSeekBar.setMax(max);
        mSeekBar.setProgress(this.findIndexOfValue(mValue));
    }

    @Override
    protected void onBindDialogView(View v) {
        super.onBindDialogView(v);
        setProgressBarValue();
    }

    @Override
    public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
        final CharSequence textToDisplay = getEntryFromValue(value);
        mValueText.setText(textToDisplay);
    }

    private CharSequence getEntryFromValue(int value) {
        CharSequence[] entries = getEntries();
        return value >= 0 && entries != null ? entries[value] : null;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void showDialog(Bundle state) {
        super.showDialog(state);

        Button positiveButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
        positiveButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        if (shouldPersist()) {
            final int progressChoice = mSeekBar.getProgress();
            setValueIndex(progressChoice);
        }

        getDialog().dismiss();
    }
}

首选项文件中的用法现在是

<com.yourfullpackage.SeekBarListPreference
        android:defaultValue="0"
        android:dialogMessage="@string/time_limit_pref"
        android:entries="@array/timeListArray"
        android:entryValues="@array/timeListValues"
        android:key="time"
        android:summary="Select time limit"
        android:title="Time"
        />

和阵列

<string-array name="timeListArray">
    <item>10 Seconds</item>
    <item>30 Seconds</item>
    <item>1 Minute</item>
    <item>2 Minutes</item>
    <item>Unlimited</item>
</string-array>

<!--This is going to be in seconds-->
<string-array name="timeListValues">
    <item>10</item>
    <item>30</item>
    <item>60</item>
    <item>120</item>
    <item>0</item>
</string-array>

如果您已经拥有ListPreferences,那么作为奖励,您不需要添加任何额外内容以将摘要显示为当前值。所以这很好用

    @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    SetSummaryForPreferenceKey(key);
}

private void SetSummaryForPreferenceKey(String key) {
    Preference preference = findPreference(key);

    // This works with our new SeekBarPreference
    if (preference instanceof ListPreference) {
        ListPreference listPref = (ListPreference) preference;
        listPref.setSummary(listPref.getEntry());
    }
}

答案 5 :(得分:1)

我对Tim的答案中提供的代码略有改进。这只是在用户移动滑块时对输出值进行实时调整,而不是要求用户单击&#34;确定&#34;按钮用于进行更改。

这对于音乐音量滑块这样的功能非常有用,用户应该可以在进行音量调节时听到音量调节。

如果用户点击&#34;确定&#34;新值保持不变。如果用户点击&#34;取消&#34;然后恢复原来的预调整值。

感谢和归功于Tim,我只是添加了额外的onClick监听器并将值更新推送到onChange监听器。

package fr.atcm.carpooling.views.utils;

/* The following code was written by Matthew Wiggins 
 * and is released under the APACHE 2.0 license 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Improvements :
 * - save the value on positive button click and/or seekbar change
 * - restore pre-adjustment value on negative button click
 */

import android.R;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class SeekBarPreference extends DialogPreference implements
        SeekBar.OnSeekBarChangeListener, OnClickListener {
    // ------------------------------------------------------------------------------------------
    // Private attributes :
    private static final String androidns = "http://schemas.android.com/apk/res/android";

    private SeekBar mSeekBar;
    private TextView mSplashText, mValueText;
    private Context mContext;

    private String mDialogMessage, mSuffix;
    private int mDefault, mMax, mValue, mOrig = 0;

    // ------------------------------------------------------------------------------------------

    // ------------------------------------------------------------------------------------------
    // Constructor :
    public SeekBarPreference(Context context, AttributeSet attrs) {

        super(context, attrs);
        mContext = context;

        // Get string value for dialogMessage :
        int mDialogMessageId = attrs.getAttributeResourceValue(androidns,
                "dialogMessage", 0);
        if (mDialogMessageId == 0)
            mDialogMessage = attrs
                    .getAttributeValue(androidns, "dialogMessage");
        else
            mDialogMessage = mContext.getString(mDialogMessageId);

        // Get string value for suffix (text attribute in xml file) :
        int mSuffixId = attrs.getAttributeResourceValue(androidns, "text", 0);
        if (mSuffixId == 0)
            mSuffix = attrs.getAttributeValue(androidns, "text");
        else
            mSuffix = mContext.getString(mSuffixId);

        // Get default and max seekbar values :
        mDefault = attrs.getAttributeIntValue(androidns, "defaultValue", 0);
        mMax = attrs.getAttributeIntValue(androidns, "max", 100);
    }

    // ------------------------------------------------------------------------------------------

    // ------------------------------------------------------------------------------------------
    // DialogPreference methods :
    @Override
    protected View onCreateDialogView() {

        LinearLayout.LayoutParams params;
        LinearLayout layout = new LinearLayout(mContext);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setPadding(6, 6, 6, 6);

        mSplashText = new TextView(mContext);
        mSplashText.setPadding(30, 10, 30, 10);
        if (mDialogMessage != null)
            mSplashText.setText(mDialogMessage);
        layout.addView(mSplashText);

        mValueText = new TextView(mContext);
        mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
        mValueText.setTextSize(32);
        params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.addView(mValueText, params);

        mSeekBar = new SeekBar(mContext);
        mSeekBar.setOnSeekBarChangeListener(this);
        layout.addView(mSeekBar, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        if (shouldPersist())
            mValue = getPersistedInt(mDefault);

        mSeekBar.setMax(mMax);
        mSeekBar.setProgress(mValue);

        return layout;
    }

    @Override
    protected void onBindDialogView(View v) {
        super.onBindDialogView(v);
        mSeekBar.setMax(mMax);
        mSeekBar.setProgress(mValue);
    }

    @Override
    protected void onSetInitialValue(boolean restore, Object defaultValue) {
        super.onSetInitialValue(restore, defaultValue);
        // Set adjustable value
        if (restore)
            mValue = shouldPersist() ? getPersistedInt(mDefault) : 0;
        else
            mValue = (Integer) defaultValue;
        // Set original pre-adjustment value
        mOrig = mValue;
    }

    // ------------------------------------------------------------------------------------------

    // ------------------------------------------------------------------------------------------
    // OnSeekBarChangeListener methods :
    @Override
    public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
        String t = String.valueOf(value);
        mValueText.setText(mSuffix == null ? t : t.concat(" " + mSuffix));

        if (shouldPersist()) {
            mValue = mSeekBar.getProgress();
            persistInt(mSeekBar.getProgress());
            callChangeListener(Integer.valueOf(mSeekBar.getProgress()));
        }

    }

    @Override
    public void onStartTrackingTouch(SeekBar seek) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seek) {
    }

    public void setMax(int max) {
        mMax = max;
    }

    public int getMax() {
        return mMax;
    }

    public void setProgress(int progress) {
        mValue = progress;
        if (mSeekBar != null)
            mSeekBar.setProgress(progress);
    }

    public int getProgress() {
        return mValue;
    }

    // ------------------------------------------------------------------------------------------

    // ------------------------------------------------------------------------------------------
    // Set the positive button listener and onClick action : 
    @Override
    public void showDialog(Bundle state) {

        super.showDialog(state);

        Button positiveButton = ((AlertDialog) getDialog())
                .getButton(AlertDialog.BUTTON_POSITIVE);
        Button negativeButton = ((AlertDialog) getDialog())
                .getButton(AlertDialog.BUTTON_NEGATIVE);
        positiveButton.setOnClickListener(cListenPos);
        negativeButton.setOnClickListener(cListenNeg);
    }

    View.OnClickListener cListenPos = new View.OnClickListener() {
        public void onClick(View v) {
            if (shouldPersist()) {
                mValue = mSeekBar.getProgress();
                mOrig = mSeekBar.getProgress();
                persistInt(mSeekBar.getProgress());
                callChangeListener(Integer.valueOf(mSeekBar.getProgress()));
            }
            ((AlertDialog) getDialog()).dismiss();
        }
    };

    View.OnClickListener cListenNeg = new View.OnClickListener() {
        public void onClick(View v) {
            if (shouldPersist()) {
                mValue = mOrig;
                persistInt(mOrig);
                callChangeListener(Integer.valueOf(mOrig));
            }
            ((AlertDialog) getDialog()).dismiss();
        }
    };

    @Override
    public void onClick(View v) {}

    // ------------------------------------------------------------------------------------------
}

答案 6 :(得分:1)

蒂姆的答案及其衍生物的一个小改进:

protected void onBindDialogView(View v) {
    super.onBindDialogView(v);
    mSeekBar.setMax(mMax);
    mSeekBar.setProgress(mValue);
    String t = String.valueOf(mValue);
    mValueText.setText(mSuffix == null ? t : t.concat(" " + mSuffix));
}

它会在启动时填充当前值,否则为空,直到您实际滑动滑块。

答案 7 :(得分:0)

如果您已经移至 androidX ,则只需使用 androidx.preference.SeekBarPreference 。文档herehere

像这样在XML中定义它:

<SeekBarPreference
        app:defaultValue="8"
        app:key="COUNT_SPEED"
        app:title="Fast count speed"
        />

enter image description here

注意:现在,在Android Studio(我当前的版本是3.2.1)中,自动建议不适用于prefs.xml中的androidx.preference.PreferenceScreen,但不必担心设置膨胀带有setPreferencesFromResource()的xml可以正常工作。您可以找到here的所有必需的属性名称和组件引用。