进度条(ProgressBar)
中常用的方法如下:
ProgressBar.setMax(int max);设置总长度为100
ProgressBar.setProgress(int progress);设置已经开启长度为0,假设设置为50,进度条将进行到一半停止。
实例:
< xml version="1.0" encoding="utf-8" >
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="进度条演示" />
android:id="@+id/probarId1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="1000"
android:progress="100" />
android:id="@+id/probarId2"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_marginTop="50dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100000"
android:progress="100"
android:secondaryProgress="300"
/>
////////////////// ProgressBarActivity///////////////////
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ProgressBar;
public class ProgressBarActivity extends Activity {
ProgressBar pro = null;
int i = 0;
int proMax = 0;
Handler handler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
}
private void findViews() {
pro = (ProgressBar) this.findViewById(R.id.probarId2);
proMax = pro.getMax();
new Thread(new Runnable() {
public void run() {
while (i < proMax) {
i=dowork();
handler.post(new Runnable() {
public void run() {
pro.setProgress(i);
}
});
}
}
}
).start();
}
public int dowork(){
Log.d("tag", String.valueOf(i));
return i+=1;
}
/*
* //不能用
*
* @Override public void run() { // TODO Auto-generated method stub while
* (i++ < proMax) { pro.setProgress(i); try { Thread.sleep(50); } catch
* (InterruptedException e) { // TODO Auto-generated catch block
* e.printStackTrace(); } } }
*/
}
拖动条(SeekBar)
进度条只能显示进度,用户不能与程序交互,通过对其操作来改变其值。而拖动条就可以实现此功能。拖动条比较常见,如“千千静听”中的播放进度条就是一个拖动条。Android平台中的SeekBar 是ProgressBar 的间接子类。
SeekBar.getProgress():获取拖动条当前值
调用setOnSeekBarChangeListener() 方法,处理拖动条值变化事件, 把SeekBar.OnSeekBarChangeListener 实例作为参数传入
实例:
< xml version="1.0" encoding="utf-8" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="1000"
android:id="@+id/seekbarId"
/>
////////////////// SeekBarActivity/////////////////
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class SeekBarActivity extends Activity implements OnSeekBarChangeListener{
SeekBar seekbar = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar);
findViews();
}
private void findViews() {
seekba