2013-04-12 115 views

回答

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); 
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); 
2

試試我與滑塊的偏好at GitHub簡單但完整的例子:

screenshot

SeekBarPreference.java(保存SeekBar編RESS爲整數值):

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>