• 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>
  • 相关阅读:
    程序打包
    MFC AfxMessageBox默认标题修改
    Json
    agsXMPP
    xmpp
    afxcomctl32.h与afxcomctl32.inl报错
    jQuery使用
    EChart使用
    C++ tinyXML使用
    electron之Windows下使用 html js css 开发桌面应用程序
  • 原文地址:https://www.cnblogs.com/jishu/p/2226823.html
Copyright © 2020-2023  润新知