请告诉我如何在Android中的偏好设置中保存SeekBar进度,以及如何在用户再次打开时将该保存的值显示在搜索栏上。
答案 0 :(得分:2)
尝试使用滑块首选项at 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>
答案 1 :(得分: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);
答案 2 :(得分: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);
答案 3 :(得分:0)
公共类MainActivity扩展了首选项实现OnSeekBarChangeListener { 私人SeekBar mSeekBar; private 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);
}
}
答案 4 :(得分:0)
我为您提供了 xml 和 java 代码,以保存 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));
}
}