Android详细教程(基础篇):十一、View组件高级篇:SeekBar 详解

7.5. SeekBar (拖动条)

  • 掌握拖动条:SeekBar的主要作用
  • 掌握SeekBar组件的事件处理操作
  • 可以使用SeekBar组件控制手机屏幕的亮度。

Android详细教程(基础篇):十一、View组件高级篇:SeekBar 详解_第1张图片

public class

SeekBar

extends AbsSeekBar

java.lang.Object

   

android.view.View

 

   

android.widget.ProgressBar

 

 

   

android.widget.AbsSeekBar

 

 

 

   

android.widget.SeekBar

 

 

Android详细教程(基础篇):十一、View组件高级篇:SeekBar 详解_第2张图片

Android详细教程(基础篇):十一、View组件高级篇:SeekBar 详解_第3张图片

 

范例一:简单拖动

配置:

"http://schemas.android.com/apk/res/android"

     android:orientation="vertical" android:layout_width="fill_parent"

     android:layout_height="fill_parent">

    

          android:id="@+id/seekbar"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content" />

     <TextView

          android:id="@+id/text"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"/>

 

Activity:

package com.makyan.demo;

import android.app.Activity;

import android.os.Bundle;

import android.text.method.ScrollingMovementMethod;

import android.widget.SeekBar;

import android.widget.TextView;

public class SeekBarActivity extends Activity {

     private SeekBar seekbar = null;

     private TextView text = null;

     @Override

     public void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

          super.setContentView(R.layout.activity_seek_bar);

          seekbar = (SeekBar) super.findViewById(R.id.seekbar); // 取得组件

          text = (TextView) super.findViewById(R.id.text); // 取得组件

          text.setMovementMethod(ScrollingMovementMethod.getInstance()); // 文本组件的内容可以滚动

          seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImpl());

     }

     private class OnSeekBarChangeListenerImpl implements

               SeekBar.OnSeekBarChangeListener {

          @Override

          public void onStartTrackingTouch(SeekBar seekBar) {

               SeekBarActivity.this.text.append("*** 开始拖动,当前值:"

                         + seekBar.getProgress() + "\n");

          }

          @Override

          public void onProgressChanged(SeekBar seekBar, int progress,

                    boolean fromUser) {

               SeekBarActivity.this.text.append("*** 正在拖动,当前值:"

                         + seekBar.getProgress() + "\n");

          }

          @Override

          public void onStopTrackingTouch(SeekBar seekBar) {

               SeekBarActivity.this.text.append("*** 停止拖动,当前值:"

                         + seekBar.getProgress() + "\n");

          }

     }

}

演示效果:

Android详细教程(基础篇):十一、View组件高级篇:SeekBar 详解_第4张图片

范例二:拖动显示图片

配置:

"http://schemas.android.com/apk/res/android"

     android:orientation="vertical" android:layout_width="fill_parent"

     android:layout_height="fill_parent">

     <ImageView

          android:id="@+id/pic"

          android:src="@drawable/pic_0"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"/>

    

          android:id="@+id/seekbar"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content" />

Activity:

package com.makyan.demo;

import android.app.Activity;

import android.os.Bundle;

import android.widget.ImageView;

import android.widget.SeekBar;

public class SeekBarActivity extends Activity {

     private SeekBar seekbar = null;

     private ImageView pic = null;

     private int picData[] = new int[] { R.drawable.pic_0, R.drawable.pic_1,

               R.drawable.pic_2, R.drawable.pic_3, R.drawable.pic_4,

               R.drawable.pic_5, R.drawable.pic_6, R.drawable.pic_7,

               R.drawable.pic_8, R.drawable.pic_9 };

     @Override

     public void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

          super.setContentView(R.layout.activity_seek_bar);

          seekbar = (SeekBar) super.findViewById(R.id.seekbar); // 取得组件

          pic = (ImageView) super.findViewById(R.id.pic); // 取得组件

          seekbar.setMax(9); // 0 ~ 9的范围

          seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImpl());

     }

     private class OnSeekBarChangeListenerImpl implements

               SeekBar.OnSeekBarChangeListener {

          @Override

          public void onStartTrackingTouch(SeekBar seekBar) {

          }

          @Override

          public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {

               pic.setImageResource(SeekBarActivity.this.picData[seekBar.getProgress()]);// 设置显示图片

          }

          @Override

          public void onStopTrackingTouch(SeekBar seekBar) {

          }

     }

}

演示效果:

Android详细教程(基础篇):十一、View组件高级篇:SeekBar 详解_第5张图片

范例三:改变屏幕亮度

WindowManager.LayoutParams layoutParams = super.getWindow().getAttributes() ;

         // 取得屏幕的属性

配置:

            android:orientation="vertical" android:layout_width="fill_parent"

            android:layout_height="fill_parent">

           

                       android:id="@+id/pic"

                       android:src="@drawable/android_book"

                       android:layout_width="fill_parent"

                       android:layout_height="wrap_content"/>

           

                       android:id="@+id/seekbar"

                       android:layout_width="fill_parent"

                       android:layout_height="wrap_content" />

Activity:

package com.makyan.demo;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.WindowManager;

import android.widget.SeekBar;

 

public class SeekBarActivity extends Activity {

 

     private SeekBar seekbar = null;

 

     @Override

     public void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

          super.setContentView(R.layout.activity_seek_bar);

          seekbar = (SeekBar) super.findViewById(R.id.seekbar); // 取得组件

          seekbar.setMax(100);

          seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImpl());

     }

 

     private class OnSeekBarChangeListenerImpl implements SeekBar.OnSeekBarChangeListener {

          @Override

          public void onStartTrackingTouch(SeekBar seekBar) {

          }

 

          @Override

          public void onProgressChanged(SeekBar seekBar, int progress,

                    boolean fromUser) {

               setScreenBrightness((float) seekBar.getProgress() / 100);

          }

 

          @Override

          public void onStopTrackingTouch(SeekBar seekBar) {

          }

     }

     private void setScreenBrightness(float num) { // 0 ~ 1表示亮度

          WindowManager.LayoutParams layoutParams = super.getWindow().getAttributes() ;    // 取得屏幕的属性

          layoutParams.screenBrightness = num ;   // 设置屏幕亮度

          super.getWindow().setAttributes(layoutParams) ;    // 重新设置窗口的属性

     }

 

}

 

演示效果就不贴了,很简单。

 

 

你可能感兴趣的:(Android详解,Android移动开发详解)