更多精彩内容,请点击阅读:《API Demos 2.3 学习笔记》
SeekBar 控件最经典的应用是在播放器中用于显示/改变播放进度的进度条。下面是一个简单的SeekBar 控件:
<SeekBar android:id="@+id/seek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="75" />
注:android:max="100" 属性表示该SeekBar 的最大值为100,即取值范围为0~100
android:progress="50" 属性表示当前初始的进度值为50
android:secondaryProgress="75" 属性表示当前初始的第二进度值为 75
在播放器的进度条中, android:progress常用来表示当前播放的进度,而 android:secondaryProgress则常用来表示当前音频/视频文件缓冲的进度。
SeekBar 主要通过设置监听器OnSeekBarChangeListener来响应用户点击的。具体使用方法如下:
// 通过findViewById方法获得RatingBar对象
SeekBar mSeekBar = (SeekBar) findViewById(R.id.seekbar);
//创建SeekBar 监听器
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO 用户停止对seekbar进行触屏操作时触发响应
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO 用户开始对seekbar进行触屏操作时触发响应
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO 用户移动滑动条,导致进度值改变时,触发响应
}
});
注:创建 SeekBar 监听器时,需要注意的是,3个参数以及它们对应的含义如下:
seekBar: 由于多个SeekBar可以同时指定同一个SeekBar 监听器 。该参数就是当前触发SeekBar 监听器的那一个SeekBar 对象。
progress: 当前进度值。取值范围从0到android:max属性所设定的最大值。
fromUser: 如果触发监听器的是来自用户触屏点击或轨迹球左右移动,则为true。
下面我们进行实例代码解析:
res-layout-seekbar_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 一个SeekBar对象-->
<!-- android:max="100" 表示seekbar最大值是100,即取值范围0~100-->
<!-- android:progress="50" 表示seekbar当前进度取值是50-->
<!-- android:secondaryProgress="75" 表示seekbar当前第二进度取值是75-->
<SeekBar android:id="@+id/seek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="75" />
<!-- 一个TextView对象,用于显示当前的progress进度值 -->
<TextView android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- 一个TextView对象,用于显示用户当前是否在对Seek Bar进行触屏操作 -->
<TextView android:id="@+id/tracking"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
src-com.example.android.apis.view-SeekBar1.java
package com.example.android.apis.view;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import com.example.android.apis.R;
/**
* 演示如何使用seek bar
*/
public class SeekBar1 extends Activity implements SeekBar.OnSeekBarChangeListener {
SeekBar mSeekBar;
TextView mProgressText;
TextView mTrackingText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar_1);
// 通过findViewById方法获得一个SeekBar对象和两个用于提示作用的TextView
mSeekBar = (SeekBar)findViewById(R.id.seek);
mSeekBar.setOnSeekBarChangeListener(this); //设置Seek Bar监听器
mProgressText = (TextView)findViewById(R.id.progress);
mTrackingText = (TextView)findViewById(R.id.tracking);
}
//当用户用手移动滑动条,改变进度值时,触发该响应。progress表示当前进度值。
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
mProgressText.setText(progress + " " +
getString(R.string.seekbar_from_touch) + "=" + fromTouch);
}
//用户开始对SeekBar进行触屏操作时触发响应
public void onStartTrackingTouch(SeekBar seekBar) {
mTrackingText.setText(getString(R.string.seekbar_tracking_on));
}
//用户停止对SeekBar进行触屏操作时触发响应
public void onStopTrackingTouch(SeekBar seekBar) {
mTrackingText.setText(getString(R.string.seekbar_tracking_off));
}
}
知识点1:关于如何自定义SeekBar的颜色,大小,图片等进阶知识,请点击阅读《SeekBar自定义(颜色,大小,图片)》
预览效果:
欢迎大家关注我的微信公众号:
微信公众号:sn0wdr1am