在Android中有四種基本的數據保存方式: 1) SharedPreference 2) 文件 3) SQLite 4) ContentProvider,而一般我們在製作Settings頁面時,透過SharedPreference可以很便利的完成儲存各種設定值,並且開發者不用顧到"儲存"的動作,Android自己幫你做完了,相當便利。 然而在實做Settings的PreferenceActivity當中,所提供的Preference Control 只有 1) CheckBoxPreference 2) EditTextPreference 3) ListPreference 4) RingtonePreference,這對開發者來說應該是不太夠,所以此篇將在PreferenceActitivy中實做SeekBar Preference的效果,當然透過此篇方法你也能自訂自己的Preference Control。
另外,在SeekBar中想要設定最大值,可透過setMax()即可,但Seekbar卻沒有設定最小值的方法,而此篇也提供簡單方式來達到在SeekBar中設定最小值,原始碼如下:
AutoCamera_SeekBarPreference.java
package com.AutoCamara; import android.app.AlertDialog.Builder; import android.content.Context; import android.preference.DialogPreference; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.TextView; import android.widget.SeekBar.OnSeekBarChangeListener; public class AutoCamera_SeekbarPreference extends DialogPreference { private Context context; private TextView mTextView; private SeekBar timeBar; private LinearLayout layout=null; private int Max_Value=30; //設定SeekBar之最大值 private int Min_Value=5; //設定SeekBar之最小值 public AutoCamera_SeekbarPreference(Context context, AttributeSet attrs) { super(context, attrs); //============== this.context=context; } @Override protected void onPrepareDialogBuilder(Builder builder) { super.onPrepareDialogBuilder(builder); //=========================== //layout之定義 layout=new LinearLayout(context); layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); layout.setMinimumWidth(400); layout.setPadding(20, 20, 20, 20); layout.setOrientation(1); // 1 is vertical //TextView之定義 mTextView=new TextView(context); mTextView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)); //Seekbar之定義 timeBar=new SeekBar(context); timeBar.setOnSeekBarChangeListener(listener); timeBar.setMax(Max_Value); //設定目前SeekBar之最大值 timeBar.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)); timeBar.setProgress(getPersistedInt(10)); //設定目前SeekBar初始值 //加入元件於Layout中 layout.addView(timeBar); layout.addView(mTextView); builder.setView(layout); } OnSeekBarChangeListener listener=new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //當progress小於Min_Value時,將目前SeekBar之Progress設為Min_Value //所以使用者將無法選取到比Min_Value更小的值,以達到設定SeekBar之最小值 //此範例設定SeekBar的選取範圍在 5-30之間 if(progress<Min_Value) { timeBar.setProgress(Min_Value); } else { mTextView.setText(String.valueOf(progress)+" (s)"); } } }; @Override protected void onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); if(positiveResult) { persistInt(timeBar.getProgress()); //關閉對話框之後將紀錄Progress值 } } }
使用方式: 在Xml資料夾中添加一Preference.xml,並將定義之元件加入
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <[Your Package Name].AutoCamera_SeekbarPreference android:key="Video_timebar" android:title="請選擇秒數" android:dialogTitle="請選擇拍照週期" android:persistent="true" android:dependency="Auto_Video"> </[Your Package Name].AutoCamera_SeekbarPreference> </PreferenceScreen>
在PreferenceActivity頁面中加入xml:
package com.AutoCamera; import android.os.Bundle; import android.preference.PreferenceActivity; public class AutoCamera_Settings extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preference); } }
沒有留言:
張貼留言