2

请告诉我如何在 Android 的“首选项”中保存 SeekBar 进度,以及如何在用户再次打开时将保存的值显示在 seekbar 上。

4

5 回答 5

2

在 GitHub 上尝试我的带有滑块首选项的简单但完整的示例:

截屏

SeekBarPreference.java(将 SeekBar 进度保存为整数值):

public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {
    private SeekBar mSeekBar;
    private int mProgress;

    public SeekBarPreference(Context context) {
        this(context, null, 0);
    }

    public SeekBarPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setLayoutResource(R.layout.preference_seekbar);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        mSeekBar = (SeekBar) view.findViewById(R.id.seekbar);
        mSeekBar.setProgress(mProgress);
        mSeekBar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (!fromUser)
            return;

        setValue(progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // not used
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // not used
    }

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        setValue(restoreValue ? getPersistedInt(mProgress) : (Integer) defaultValue);
    }

    public void setValue(int value) {
        if (shouldPersist()) {
            persistInt(value);
        }

        if (value != mProgress) {
            mProgress = value;
            notifyChanged();
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getInt(index, 0);
    }
}

preference_seekbar.xml(带有 SeekBar 和 TextView 的布局文件):

<?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:gravity="center_vertical" >

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="0dp"
        android:layout_weight="80"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@android:id/summary"
        android:layout_width="0dp"
        android:layout_weight="20"
        android:layout_height="wrap_content" />

</LinearLayout>
于 2015-05-15T20:51:17.417 回答
1

我给你xmljava代码来保存SeekBar的状态以及TextView ,它使用SharedPreferences类显示SeekBar的值。

xml代码:

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

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center_horizontal"
        android:textSize="30sp"
        tools:text="0" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp" />

</LinearLayout>

Java代码:

import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private SeekBar seekBar;

    private int seekBarProgress;

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

        textView = (TextView) findViewById(R.id.textview);
        seekBar = (SeekBar) findViewById(R.id.seekBar);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                textView.setText(String.valueOf(i));
                saveData();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        loadData();
        updateViews();
    }
    public void saveData() {
        SharedPreferences sharedPreferences = getSharedPreferences("my_preference", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("my_seekBar", seekBar.getProgress());
        editor.apply();
    }
    public void loadData() {
        SharedPreferences sharedPreferences = getSharedPreferences("my_preference", MODE_PRIVATE);
        seekBarProgress = sharedPreferences.getInt("my_seekBar",0);
    }
    public void updateViews() {
        seekBar.setProgress(seekBarProgress);
        textView.setText(String.valueOf(seekBarProgress));
    }
}
于 2020-07-24T13:36:51.367 回答
0

保存进度

SharedPreference mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor mEditor = mSharedPrefs.edit();
int mProgress = mSeekBar.getProgress();
mEditor.putInt("mMySeekBarProgress", mProgress).commit();

而不是你可以这样做来得到它:

int mProgress = mSharedPrefs.getInt("mMySeekBarProgress", 0);
mSeekBar.setProgress(mProgress);
于 2013-04-12T11:16:41.630 回答
0

初始化一个 SharedPreferences 对象:

SharedPreferences pre = getSharedPreferences("pre_name", Context.MODE_PRIVATE);

在首选项中添加一个值:

pre.edit().putInt("KEY_PROGRESS_VALUE", 30).commit();

从首选项中加载一个值:

int progress = pre.getInt("KEY_PROGRESS_VALUE",0); // 0: default value

然后设置进度条的进度:

progrssBar.setProgress(progress);
于 2013-04-12T11:18:29.573 回答
0

公共类 MainActivity 扩展 Preference 实现 OnSeekBarChangeListener { private SeekBar mSeekBar; 私人int mProgress;

public MainActivity(Context context) {
    this(context, null, 0);
}

public MainActivity(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public MainActivity(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setLayoutResource(R.layout.preference_seekbar);
}

@Override
protected void onBindView(View view) {
    super.onBindView(view);
    mSeekBar = (SeekBar) view.findViewById(R.id.seekbar);
    mSeekBar.setProgress(mProgress);
    mSeekBar.setOnSeekBarChangeListener(this);
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if (!fromUser)
        return;

    setValue(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // not used
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // not used
}

@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
    setValue(restoreValue ? getPersistedInt(mProgress) : (Integer) defaultValue);
}

public void setValue(int value) {
    if (shouldPersist()) {
        persistInt(value);
    }

    if (value != mProgress) {
        mProgress = value;
        notifyChanged();
    }
}

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
    return a.getInt(index, 0);
}

}

于 2018-09-28T05:08:16.397 回答