SeekBar使用方法

概述

本文讨论SeekBar的使用方法。

http://blog.csdn.net/a_flying_bird/article/details/50948916一文讨论了Handler的使用方法,这是本文的基础。本文使用同样的示例,即一个计算任务。

Class Overview

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.

Clients of the SeekBar can attach a SeekBar.OnSeekBarChangeListener to be notified of the user’s actions.

示例

采用和Handler使用方法一样的例子,只是把TextView换成了SeekBar。

效果

seekbar

my_layout.xml

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

    <Button
        android:id="@+id/start"
        android:layout_width="192dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/start" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

字符串

<string name="start">Start</string>

代码

省略掉自动生成的代码

public class MainActivity extends Activity {
    protected static final String TAG = "MainActivity";
    private Button button = null;
    private SeekBar seekBar = null;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int count = msg.what;
            seekBar.setProgress(count);
        }
    };

    private Runnable adding = new Runnable() {

        @Override
        public void run() {
            double d;
            for (int count = 1; count <= 1000; count++) {
                for (int i = 0; i < 1000; i++) {
                    d = Math.sqrt(Math.sqrt(i));
                    Log.d(TAG, "count = " + count + ", sqrt(sqrt(" + i + "))=" + d);
                }
                if (count % 10 == 0) {
                    handler.sendEmptyMessage(count / 100);
                }
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

        button = (Button)findViewById(R.id.start);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                calculate();
            }
        });

        seekBar = (SeekBar)findViewById(R.id.seekBar);
        seekBar.setMax(10);
    }

    protected void calculate() {
        Thread thread = new Thread(adding);
        thread.start();
    }

要点

如下:
- 初始化最大范围:void setMax(int max)
- 更新进度:void setProgress(int progress)

更多事件处理

SeekBar另一个常用的方法是:

/**
 * Sets a listener to receive notifications of changes to the SeekBar's progress level. Also
 * provides notifications of when the user starts and stops a touch gesture within the SeekBar.
 * 
 * @param l The seek bar notification listener
 * 
 * @see SeekBar.OnSeekBarChangeListener
 */
public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
    mOnSeekBarChangeListener = l;
}

其中用到的接口如下:

/**
 * A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch
 * the thumb and drag left or right to set the current progress level or use the arrow keys.
 * Placing focusable widgets to the left or right of a SeekBar is discouraged. 
 * <p>
 * Clients of the SeekBar can attach a {@link SeekBar.OnSeekBarChangeListener} to
 * be notified of the user's actions.
 *
 * @attr ref android.R.styleable#SeekBar_thumb
 */
public class SeekBar extends AbsSeekBar {

    /**
     * A callback that notifies clients when the progress level has been
     * changed. This includes changes that were initiated by the user through a
     * touch gesture or arrow key/trackball as well as changes that were initiated
     * programmatically.
     */
    public interface OnSeekBarChangeListener {

        /**
         * Notification that the progress level has changed. Clients can use the fromUser parameter
         * to distinguish user-initiated changes from those that occurred programmatically.
         * 
         * @param seekBar The SeekBar whose progress has changed
         * @param progress The current progress level. This will be in the range 0..max where max
         *        was set by {@link ProgressBar#setMax(int)}. (The default value for max is 100.)
         * @param fromUser True if the progress change was initiated by the user.
         */
        void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser);

        /**
         * Notification that the user has started a touch gesture. Clients may want to use this
         * to disable advancing the seekbar. 
         * @param seekBar The SeekBar in which the touch gesture began
         */
        void onStartTrackingTouch(SeekBar seekBar);

        /**
         * Notification that the user has finished a touch gesture. Clients may want to use this
         * to re-enable advancing the seekbar. 
         * @param seekBar The SeekBar in which the touch gesture began
         */
        void onStopTrackingTouch(SeekBar seekBar);
    }

示例:

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        Toast.makeText(MainActivity.this, 
                "onProgressChanged()", 
                Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        Toast.makeText(MainActivity.this, 
                "onStartTrackingTouch()", 
                Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        Toast.makeText(MainActivity.this, 
                "onStopTrackingTouch()", 
                Toast.LENGTH_SHORT)
                .show(); 

    }

});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用SeekBar控制Android手机音量的一般步骤: 1. 在布局文件中添加SeekBar 在布局文件中添加SeekBar组件,例如: ``` <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" /> ``` 其中,max属性表示SeekBar的最大值,progress属性表示SeekBar的初始值。 2. 在Activity中获取SeekBar对象 在Activity中获取SeekBar对象,例如: ``` SeekBar seekBar = findViewById(R.id.seekBar); ``` 3. 获取AudioManager对象 获取AudioManager对象,例如: ``` AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); ``` 4. 监听SeekBar变化 使用seekBar.setOnSeekBarChangeListener()方法监听SeekBar变化,并在onProgressChanged()方法中设置音量,例如: ``` seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); ``` 其中,setStreamVolume()方法的第一个参数表示要设置的音频流的类型,第二个参数表示要设置的音量大小,第三个参数表示是否显示系统音量控制UI。 希望这些信息可以帮助您实现使用SeekBar控制Android手机音量的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值