下面介绍SeekBarPreference控件,保存的Value为数字值,直接可以在preferences.xml文件中使用无需其他代码。

下面是控件使用到得标准Android属性

  • android:dialogMessage 显示对话框中的文本信息
  • android:text 在选择进度条后,显示的文本信息
  • android:maxSeekBar的最大值
                  /*           The following code was written by Matthew Wiggins 
* and is released under the APACHE 2.0 license
*
*
http://www.apache.org/licenses/LICENSE-2.0
*/
package com.hlidskialf.android.preference;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.preference.DialogPreference;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.LinearLayout;


public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener
{
private static final String androidns = " http://schemas.android.com/apk/res/android " ;

private SeekBar mSeekBar;
private TextView mSplashText,mValueText;
private Context mContext;

private String mDialogMessage, mSuffix;
private int mDefault, mMax, mValue = 0 ;

public SeekBarPreference(Context context, AttributeSet attrs) {
super(context,attrs);
mContext
= context;

mDialogMessage
= attrs.getAttributeValue(androidns, " dialogMessage " );
mSuffix
= attrs.getAttributeValue(androidns, " text " );
mDefault
= attrs.getAttributeIntValue(androidns, " defaultValue " , 0 );
mMax
= attrs.getAttributeIntValue(androidns, " max " , 100 );

}
@Override
protected View onCreateDialogView() {
LinearLayout.LayoutParams
params ;
LinearLayout layout
= new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(
6 , 6 , 6 , 6 );

mSplashText
= new TextView(mContext);
if (mDialogMessage != null )
mSplashText.setText(mDialogMessage);
layout.addView(mSplashText);

mValueText
= new TextView(mContext);
mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
mValueText.setTextSize(
32 );
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(mValueText,
params );

mSeekBar
= new SeekBar(mContext);
mSeekBar.setOnSeekBarChangeListener(
this );
layout.addView(mSeekBar,
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

if (shouldPersist())
mValue
= getPersistedInt(mDefault);

mSeekBar.setMax(mMax);
mSeekBar.setProgress(mValue);
return layout;
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
mSeekBar.setMax(mMax);
mSeekBar.setProgress(mValue);
}
@Override
protected void onSetInitialValue(boolean restore, Object defaultValue)
{
super.onSetInitialValue(restore, defaultValue);
if (restore)
mValue
= shouldPersist() ? getPersistedInt(mDefault) : 0 ;
else
mValue
= (Integer)defaultValue;
}

public void onProgressChanged(SeekBar seek, int value, boolean fromTouch)
{
String t
= String.valueOf(value);
mValueText.setText(mSuffix
== null ? t : t.concat(mSuffix));
if (shouldPersist())
persistInt(value);
callChangeListener(
new Integer(value));
}
public void onStartTrackingTouch(SeekBar seek) {}
public void onStopTrackingTouch(SeekBar seek) {}

public void setMax( int max) { mMax = max; }
public int getMax() { return mMax; }

public void setProgress( int progress) {
mValue
= progress;
if (mSeekBar != null )
mSeekBar.setProgress(progress);
}
public int getProgress() { return mValue; }
}

下面是在XML layout的例子

                             <          com.hlidskialf.android.preference.SeekBarPreference           android:key          ="duration"          
android:title
="Duration of something"
android:summary
="How long something will last"
android:dialogMessage
="Something duration"
android:defaultValue
="5"
android:text
=" minutes"
android:max
="60"
/>

更多相关文章

  1. 箭头函数的基础使用
  2. NPM 和webpack 的基础使用
  3. Python list sort方法的具体使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. android -------- LiveDataBus的使用
  6. Android(安卓)Support Library
  7. android使用HttpClient和URLConnection获取网页内容
  8. Android中UI设计的一些技巧!!!
  9. Android性能调优

随机推荐

  1. Android 跑马灯效果
  2. 【Android 开源系列】之缓存框架
  3. android 读取资源字符串的 方法
  4. 【转】Android最佳性能实践(一)——合理
  5. Android CPU使用率:top和dump cpuinfo的不
  6. Android:你不知道的 WebView 使用漏洞
  7. android 中遇到的Ui问题
  8. 使用Android编译环境编译小工具
  9. android定制化软件修改或添加按键驱动的
  10. Android网络抓包