a) 什么是SeekBar
b) 使用SeekBar的步骤:
i. 在布局文件当中声明SeekBar: <SekBar android:id=”@+id/seekbarld” android:layout_width=”fill_parent” android:layout_height=”wrap_content”/>
ii. 定义一个OnSeekBarChangeListener: private class SeekBarListener implements SeekBar.OnSeekBarChangeListener{public void onProgressChanged(SeekBar seekBar,int progress,Boolean fromUser){System.out.println(progress);} public void onStartTrackingTouch(SeekBar seekBar){System.out.println(“start:=>”+seekBar.getProgress());} public void onStopTrackingTouch(SeekBar seekBar){System.out.println(“begin:=>”+seekBar.getProgress());}}
iii. 为SeekBar绑定监听器: seekBarSetOnSeekBarChangeListener(new SeekBarListener());
案例项目:
main.xml:
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<SeekBar
android:id="@+id/seekBarId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
SeekBarJsd.java:
package com.jsd;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
/**
* SeekBar Progress
* @author Administrator
*
*/
public class SeekBarJsd extends Activity {
private SeekBar seekBar = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
seekBar = (SeekBar) findViewById(R.id.seekBarId);
//设置该进度条的最大值,默认情况下为O
seekBar.setMax(100);
seekBar.setOnSeekBarChangeListener(seekBarListener);
}
/**
* SeekBarListener
* 定义一个监听器,该监听器负责监听进度条进度的改变
*/
private SeekBar.OnSeekBarChangeListener seekBarListener = new SeekBar.OnSeekBarChangeListener() {
/**
* 当用户结束对滑块滑动时,调用该方法
*/
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
System.out.println("stop: > "+seekBar.getProgress());
}
/**
* 当用户开始滑动滑块时调用该方法
*/
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
System.out.println("start: => "+seekBar.getProgress());
}
/**
* 当进度条发生变化时调用该方法
*/
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
System.out.println(progress);
}
}