职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 525|回复: 2

android 自定义SeekBarPreference 实现

[复制链接]
broken 发表于 2011-7-10 09:23 | 显示全部楼层 |阅读模式


由于网上有很多人问到SeekBarPreference怎么去实现,今天将这个效果做出来,本例子并没有真正的改变屏幕亮度,如果真正想去实现,那么可以在这个类中onProgressChanged()方法或者onDialogClosed()方法中写上自己调节亮度的代码,并将这些值保存起来。
1.首先定义一个类SeekBarPreference继承于DialogPreference的类:
  1. package com.kewen.systeminfo;   
  2.   
  3. import android.content.Context;   
  4. import android.preference.DialogPreference;   
  5. import android.util.AttributeSet;   
  6. import android.util.Log;   
  7. import android.view.View;   
  8. import android.widget.SeekBar;   
  9. import android.widget.TextView;   
  10. import android.widget.SeekBar.OnSeekBarChangeListener;   
  11.   
  12. public class SeekBarPreference extends DialogPreference implements  
  13.         OnSeekBarChangeListener {   
  14.     private SeekBar seekBar;   
  15.     private TextView textView;   
  16.   
  17.     public SeekBarPreference(Context context, AttributeSet attrs) {   
  18.         super(context, attrs);   
  19.         // TODO Auto-generated constructor stub   
  20.     }   
  21.   
  22.     @Override  
  23.     protected void onBindDialogView(View view) {   
  24.         // TODO Auto-generated method stub   
  25.         super.onBindDialogView(view);   
  26.         seekBar = (SeekBar) view.findViewById(R.id.seekBar1);   
  27.         textView = (TextView) view.findViewById(R.id.textView1);   
  28.         seekBar.setOnSeekBarChangeListener(this);   
  29.     }   
  30.   
  31.     @Override  
  32.     protected void onDialogClosed(boolean positiveResult) {   
  33.         // TODO Auto-generated method stub   
  34.         if (positiveResult) {   
  35.             Log.i("Dialog closed", "You click positive button");   
  36.         } else {   
  37.             Log.i("Dialog closed", "You click negative button");   
  38.         }   
  39.     }   
  40.   
  41.     @Override  
  42.     public void onProgressChanged(SeekBar seekBar, int progress,   
  43.             boolean fromUser) {   
  44.         textView.setText(progress + "%  " + progress + "/100");   
  45.   
  46.     }   
  47.   
  48.     @Override  
  49.     public void onStartTrackingTouch(SeekBar seekBar) {   
  50.         // TODO Auto-generated method stub   
  51.   
  52.     }   
  53.   
  54.     @Override  
  55.     public void onStopTrackingTouch(SeekBar seekBar) {   
  56.         // TODO Auto-generated method stub   
  57.   
  58.     }   
  59.   
  60. }  

  61. package com.kewen.systeminfo;

  62. import android.content.Context;
  63. import android.preference.DialogPreference;
  64. import android.util.AttributeSet;
  65. import android.util.Log;
  66. import android.view.View;
  67. import android.widget.SeekBar;
  68. import android.widget.TextView;
  69. import android.widget.SeekBar.OnSeekBarChangeListener;

  70. public class SeekBarPreference extends DialogPreference implements
  71.                 OnSeekBarChangeListener {
  72.         private SeekBar seekBar;
  73.         private TextView textView;

  74.         public SeekBarPreference(Context context, AttributeSet attrs) {
  75.                 super(context, attrs);
  76.                 // TODO Auto-generated constructor stub
  77.         }

  78.         @Override
  79.         protected void onBindDialogView(View view) {
  80.                 // TODO Auto-generated method stub
  81.                 super.onBindDialogView(view);
  82.                 seekBar = (SeekBar) view.findViewById(R.id.seekBar1);
  83.                 textView = (TextView) view.findViewById(R.id.textView1);
  84.                 seekBar.setOnSeekBarChangeListener(this);
  85.         }

  86.         @Override
  87.         protected void onDialogClosed(boolean positiveResult) {
  88.                 // TODO Auto-generated method stub
  89.                 if (positiveResult) {
  90.                         Log.i("Dialog closed", "You click positive button");
  91.                 } else {
  92.                         Log.i("Dialog closed", "You click negative button");
  93.                 }
  94.         }

  95.         @Override
  96.         public void onProgressChanged(SeekBar seekBar, int progress,
  97.                         boolean fromUser) {
  98.                 textView.setText(progress + "%  " + progress + "/100");

  99.         }

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

  103.         }

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

  107.         }

  108. }
复制代码
2.以上实现的为一个对话框式的Preference,也就是SeekBar将会设置在一个DialogPreference上,以下为DialogPreference的dialogLayout文件:
Xml代码
  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" android:layout_height="fill_parent"  
  4.     android:orientation="vertical">  
  5.   
  6.     <SeekBar android:layout_width="fill_parent"  
  7.         android:layout_height="wrap_content" android:id="@+id/seekBar1"  
  8.         android:layout_marginLeft="20dip" android:layout_marginRight="10dip"  
  9.         android:max="100" android:progress="60"></SeekBar>  
  10.     <TextView android:text="TextView" android:id="@+id/textView1"  
  11.         android:layout_height="wrap_content" android:layout_width="fill_parent"  
  12.         android:layout_marginLeft="20dip" ></TextView>  
  13. </LinearLayout>  

  14. <?xml version="1.0" encoding="utf-8"?>
  15. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  16.         android:layout_width="fill_parent" android:layout_height="fill_parent"
  17.         android:orientation="vertical">

  18.         <SeekBar android:layout_width="fill_parent"
  19.                 android:layout_height="wrap_content" android:id="@+id/seekBar1"
  20.                 android:layout_marginLeft="20dip" android:layout_marginRight="10dip"
  21.                 android:max="100" android:progress="60"></SeekBar>
  22.         <TextView android:text="TextView" android:id="@+id/textView1"
  23.                 android:layout_height="wrap_content" android:layout_width="fill_parent"
  24.                 android:layout_marginLeft="20dip" ></TextView>
  25. </LinearLayout>
复制代码
3.将写好的自定义Preference类放到定义preference的xml文件中:
Xml代码
  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>  

  6. <com.kewen.systeminfo.SeekBarPreference
  7. android:dialogTitle="亮度调整" android:title="调整亮度"
  8. android:summary="调整屏幕的亮度"  android:key="light"
  9. android:dialogLayout="@layout/seekbar">
  10. </com.kewen.systeminfo.SeekBarPreference>
复制代码
以上三步为实现这个效果的关键代码,以后还会有DatePickerPreference、TimePickerPreference出现

愚人 发表于 2011-7-10 09:23 | 显示全部楼层
先收藏了。
秋秋 发表于 2011-7-24 11:18 | 显示全部楼层
越办越好~~~~~~~~~`
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-30 06:40 , Processed in 0.125822 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表