android 自定义SeekBarPreference 实现
1.首先定义一个类SeekBarPreference继承于DialogPreference的类:
package com.kewen.systeminfo;
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class SeekBarPreference extends DialogPreference implements
OnSeekBarChangeListener {
private SeekBar seekBar;
private TextView textView;商账追收案例
public SeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onBindDialogView(View view) {
// TODO Auto-generated method stub
super.onBindDialogView(view);
seekBar = (SeekBar) view.findViewById(R.id.seekBar1);
textView = (TextView) view.findViewById(R.id.textView1);
seekBar.setOnSeekBarChangeListener(this);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
// TODO Auto-generated method stub
if (positiveResult) {
Log.i("Dialog closed", "You click positive button");
} else {
Log.i("Dialog closed", "You click negative button");
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
textView.setText(progress + "% " + progress + "/100");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}2011大智慧下载
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
2.以上实现的为一个对话框式的Preference,也就是SeekBar将会旋转在一个DialogPreference上,以下为DialogPreference的dialogLayout文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<SeekBar android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/seekBar1"
android:layout_marginLeft="20dip" android:layout_marginRight="10dip"
android:max="100" android:progress="60"></SeekBar>
<TextView android:text="TextView" android:id="@+id/textView1"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_marginLeft="20dip" ></TextView>
</LinearLayout>
3.将写好的自定义Preference类放到定义preference的xml文件中:
<com.kewen.systeminfo.SeekBarPreference
android:dialogTitle="亮度调整" android:title="调整亮度"
android:summary="调整屏幕的亮度" android:key="light"
android:dialogLayout="@layout/seekbar">
</com.kewen.systeminfo.SeekBarPreference>
以上三步为实现这个效果的关键代码,以下还会有DatePickerPreference、TimePickerPreference出现