Android下的扩展SeekBar

Android系统下的SeekBar太简单,简单到连起止的提示和动态选择后变化的提示都没有。最起码我没找到,所以自己实现了一个扩展的SeekBar,应该好用很多。其效果图如下:

Android下的扩展SeekBar

贴上代码:
1、调用的方法举例:
//seek bar preference
ExtSeekBarPreference loPrefHttpRangeSize = new ExtSeekBarPreference(this, null);
loPrefHttpRangeSize.setKey(PREF_KEY_HTTP_RANGE_SIZE);
loPrefHttpRangeSize.setTitle(R.string.CONFIG_TITLE_HTTP_RANGESIZE);
loPrefHttpRangeSize.setPersistent(true);
loPrefHttpRangeSize.setMin(3);
loPrefHttpRangeSize.setMax(20);
loPrefHttpRangeSize.setKeyProgressIncrement(1);
loPrefHttpRangeSize.setUnit("00KB");
loPrefHttpRangeSize.setValue(coGlobalContext.coPrefs.getInt(PREF_KEY_HTTP_RANGE_SIZE, PREF_DEFAULT_HTTP_RANGE_SIZE));
loPrefHttpRangeSize.setSummary(coGlobalContext.coPrefs.getInt(PREF_KEY_HTTP_RANGE_SIZE, PREF_DEFAULT_HTTP_RANGE_SIZE));
loPrefHttpRangeSize.setNegativeButtonText(getResources().getString(R.string.CONFIRM_CANCEL));
loPrefHttpRangeSize.setPositiveButtonText(getResources().getString(R.string.CONFIRM_OK));
dialogBasedPrefCat.addPreference(loPrefHttpRangeSize);

2、扩展的SeekBar类:
public class ExtSeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener{

private int ciMinVal=0;
private int ciMaxVal=100;
private int ciInitVal=0;
private int ciIncrement=1;
private String csUnit="";
private Context coContext;
private TextView coSeekBarValView=null;
private SeekBar coSeekBar;

public ExtSeekBarPreference(Context aoContext, AttributeSet attrs)
{
super(aoContext, attrs);
this.coContext = aoContext;
}

public int getMin()
{
return ciMinVal;
}

public void setMin(int aiMin)
{
ciMinVal = aiMin;
}

public void setMax(int aiMax)
{
ciMaxVal = aiMax;
}

public void setUnit(String asUnit)
{
csUnit = asUnit;
}

public void setKeyProgressIncrement(int aiIncrement)
{
ciIncrement = aiIncrement;
}

public void setValue(int aiVal)
{
ciInitVal = aiVal;
}

protected void onPrepareDialogBuilder(Builder builder)
{
LayoutInflater factory = LayoutInflater.from(coContext);
View loSeekBarView = factory.inflate(R.layout.view_ext_seekbar, null);
builder.setView(loSeekBarView);

coSeekBar = (SeekBar)loSeekBarView.findViewById(R.id.SeekBarControl);
coSeekBar.setOnSeekBarChangeListener(this);
coSeekBar.setMax(ciMaxVal-ciMinVal);
coSeekBar.setKeyProgressIncrement(ciIncrement);

TextView loMinText = (TextView)loSeekBarView.findViewById(R.id.SeekBarMin);
loMinText.setText(ciMinVal+csUnit);

TextView loMaxText = (TextView)loSeekBarView.findViewById(R.id.SeekBarMax);
loMaxText.setText(ciMaxVal+csUnit);

coSeekBarValView = (TextView)loSeekBarView.findViewById(R.id.SeekBarVal);

//initial value
coSeekBar.setProgress(ciInitVal-ciMinVal);
}

public void setSummary(int aiVal)
{
String lsSummary="";

lsSummary = (aiVal) + csUnit;

super.setSummary(lsSummary);
}

protected void onDialogClosed(boolean positiveResult)
{
if (positiveResult)
{
// 保存SeekBar的值
persistInt(coSeekBar.getProgress()+ciMinVal);

// must reset initval
ciInitVal = coSeekBar.getProgress()+ciMinVal;
}

super.onDialogClosed(positiveResult);
}

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
coSeekBarValView.setText((ciMinVal+progress)+csUnit);
this.setSummary((ciMinVal+progress)+csUnit);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}
}

3、SeekBar的布局xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:paddingTop="10px"
android:paddingLeft="10px"
android:paddingRight="10px"
android:paddingBottom="10px"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<SeekBar
android:id="@+id/SeekBarControl"
android:paddingLeft="10px"
android:paddingRight="10px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:id="@+id/SeekBarMin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="18px"
android:text="Min" />

<TextView
android:id="@+id/SeekBarVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="18px"
android:text="" />

<TextView
android:id="@+id/SeekBarMax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="18px"
android:text="Max" />

</RelativeLayout>

</LinearLayout>

你可能感兴趣的:(android)