Android SeekBarPreference浅聊

     由于网上有很多人问到SeekBarPreference怎么去实现,今天将这个效果做出来,本例子并没有真正的改变屏幕亮度,如果真正想去实现,那么可以在这个类中onProgressChanged()方法或者onDialogClosed()方法中写上自己调节亮度的代码,并将这些值保存起来。

      1.首先定义一个类SeekBarPreference继承于DialogPreference的类:

java代码:

  1. package eoe.demo;
  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,booleanfromUser) {
  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文件: http://www.my400800.cn


java代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <hz.demo.SeekBarPreference
  2. android:dialogTitle="亮度调整"
  3. android:title="调整亮度"
  4. android:summary="调整屏幕的亮度"
  5. android:key="light"
  6. android:dialogLayout="@layout/seekbar">
  7. </hz.demo.SeekBarPreference>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值