android UI SeekBar

 

SeekBar类似于ProgressBar,不过ProgressBar主要功能是让用户知道目前的状态,而SeekBar的功能在于让用户调整进度。举个例子,在音乐播放器中,你可以通过SeekBar来调整音乐播放的进度。
下面是效果图:
android UI SeekBar_第1张图片
拖动时,TextView会动态显示当前的进度
布局xml代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView android:id="@+id/tv"
 	android:layout_width="fill_parent"
 	android:layout_height="wrap_content"
 	android:text=""
 />
 <SeekBar 
        android:id="@+id/seek"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100" 
        android:progress="10"
 />
</LinearLayout>

程序代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.pocketdigi;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
 
public class main extends Activity {
    /** Called when the activity is first created. */
	SeekBar sb;
	TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.tv);
        sb=(SeekBar)findViewById(R.id.seek);
        sb.setProgress(90);
        sb.setOnSeekBarChangeListener(sbLis);
    }
    private OnSeekBarChangeListener sbLis=new OnSeekBarChangeListener(){
 
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			//进度改变时触发
			tv.setText(String.valueOf(sb.getProgress()));
 
		}
 
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// 开始拖动时触发,与onProgressChanged区别在于onStartTrackingTouch在停止拖动前只触发一次
			//而onProgressChanged只要在拖动,就会重复触发
		}
 
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			//结束拖动时触发
 
		}
 
    };
}

你可能感兴趣的:(android UI SeekBar)