拖动条(SeekBar)组件与ProgressBar水平形式显示的进度条比较相似,不过其最大的区别在于,
拖动条可以由用户组件进行手工调节,例如:调整播放音量和播放进度时会用到拖动条。
在main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<SeekBar
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"/>
</LinearLayout>
在MySeekBarDemo.java程序中
package com.tarena.seekbar;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MySeekBarDemo extends Activity {
private SeekBar seekbar = null;
private TextView text = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.seekbar = (SeekBar) super.findViewById(R.id.seekbar); //取得组件
this.text = (TextView) super.findViewById(R.id.text); //取得组件
this.seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImpl());
this.text.setMovementMethod(ScrollingMovementMethod.getInstance()); //设置文本组件可以滚动
}
private class OnSeekBarChangeListenerImpl implements OnSeekBarChangeListener{
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
MySeekBarDemo.this.text.append("***开始拖动,当前值为:"
+ seekBar.getProgress() + "\n");
}
public void onStartTrackingTouch(SeekBar seekBar) {
MySeekBarDemo.this.text.append("***正在拖动,当前值为:"
+ seekBar.getProgress() + "\n");
}
public void onStopTrackingTouch(SeekBar seekBar) {
MySeekBarDemo.this.text.append("***停止拖动,当前值为:"
+ seekBar.getProgress() + "\n");
}
}
}