Android 自定义SeekBarPreference实现

来源:互联网 发布:手机淘宝如何注册账号 编辑:程序博客网 时间:2024/05/17 09:29
由于网上有很多人问到SeekBarPreference怎么去实现,今天将这个效果做出来,本例子并没有真正的改变屏幕亮度,如果真正想去实现,那么可以在这个类中onProgressChanged()方法或者onDialogClosed()方法中写上自己调节亮度的代码,并将这些值保存起来。
       1.首先定义一个类SeekBarPreference继承于DialogPreference的类:

java代码:

  1. package eoe.systeminfo;


  2. import android.content.Context;
  3. import android.preference.DialogPreference;
  4. import android.util.AttributeSet;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.SeekBar;
  8. import android.widget.TextView;
  9. import android.widget.SeekBar.OnSeekBarChangeListener;


  10. public class SeekBarPreference extends DialogPreference implements
  11. OnSeekBarChangeListener {
  12. private SeekBar seekBar;
  13. private TextView textView;
  14. public SeekBarPreference(Context context, AttributeSet attrs) {
  15. super(context, attrs);
  16. // TODO Auto-generated constructor stub
  17. }


  18. @Override
  19. protected void onBindDialogView(View view) {
  20. // TODO Auto-generated method stub
  21. super.onBindDialogView(view);
  22. seekBar = (SeekBar) view.findViewById(R.id.seekBar1);
  23. textView = (TextView) view.findViewById(R.id.textView1);
  24. seekBar.setOnSeekBarChangeListener(this);
  25. }


  26. @Override
  27. protected void onDialogClosed(boolean positiveResult) {
  28. // TODO Auto-generated method stub
  29. if (positiveResult) {
  30. Log.i("Dialog closed", "You click positive button");
  31. } else {
  32. Log.i("Dialog closed", "You click negative button");
  33. }
  34. }


  35. @Override
  36. public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
  37. textView.setText(progress + "% " + progress + "/100");
  38. }


  39. @Override
  40. public void onStartTrackingTouch(SeekBar seekBar) {
  41. // TODO Auto-generated method stub
  42. }


  43. @Override
  44. public void onStopTrackingTouch(SeekBar seekBar) {
  45. // TODO Auto-generated method stub
  46. }

  47. }
复制代码

         2.以上实现的为一个对话框式的Preference,也就是SeekBar将会设置在一个DialogPreference上,以下为DialogPreference的dialogLayout文件:

java代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <SeekBar
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:id="@+id/seekBar1"
  10. android:layout_marginLeft="20dip"
  11. android:layout_marginRight="10dip"
  12. android:max="100"
  13. android:progress="60">
  14. </SeekBar>
  15. <TextView
  16. android:text="TextView"
  17. android:id="@+id/textView1"
  18. android:layout_height="wrap_content"
  19. android:layout_width="fill_parent"
  20. android:layout_marginLeft="20dip" >
  21. </TextView>
  22. </LinearLayout>
复制代码

       3.将写好的自定义Preference类放到定义preference的xml文件中:

java代码:
  1. <com.kewen.systeminfo.SeekBarPreference
  2. android:dialogTitle="亮度调整" android:title="调整亮度"
  3. android:summary="调整屏幕的亮度" android:key="light"
  4. android:dialogLayout="@layout/seekbar">
  5. </com.kewen.systeminfo.SeekBarPreference>
复制代码

转自:Android 自定义SeekBarPreference实现
原创粉丝点击