main.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progressbar1" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressbar1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progressbar2" />
<!-- 注:进度条默认为环形的, 假如要定义水平进度条时,需要加上属性 style="?android:attr/progressBarStyleHorizontal-->
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressbar2"
/>
</LinearLayout>
.java 程序
package com.example.progressbardemo;
import java.security.PublicKey;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private ProgressBar mProgressBar;
private int mProgressStatus = 0;
//创建一个Handler对象
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressBar = (ProgressBar) findViewById(R.id.progressbar2);
//设定进度条的最大值,其将为该进度条显示的基数
mProgressBar.setMax(1000);
//新创建一个线程
new Thread( new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//循环1000次,不停更新mprogresstatus 的值
while(mProgressStatus++<1000)
{
//将一个runnable对象添加到消息队列中,并且当执行到该对象时执行run方法
mHandler.post(new Runnable() { //自定义
@Override
public void run() {
// TODO Auto-generated method stub
//重新设置进度条当前的值
mProgressBar.setProgress(mProgressStatus);
}
});
}
}
}).start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
拖动条(SeekBar)
seekbar 是 ProgressBar的一个子类
有几个方法:
SeekBar.getProgress() 获取拖动条当前值
setOnSeekBarChangeListener()方法,处理拖动条值变化事件,把SeekBar.OnSeekBarChangeListener实例作为参数传入
string.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">拖动条实例</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="speed">播放进度</string>
</resources>
main.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/speed"
android:id="@+id/speed"
/>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/seek_bar"
/>
</LinearLayout>
1: package com.example.seekbardemo;
2:
3: import android.R.bool;
4: import android.os.Bundle;
5: import android.os.Handler;
6: import android.app.Activity;
7: import android.view.Menu;
8: import android.widget.SeekBar;
9: import android.widget.TextView;
10:
11: public class MainActivity extends Activity {
12: private SeekBar seekBar;
13: private TextView textView;
14: //标记是否需要更新
15: private boolean flag = true;
16: private Handler handler = new Handler();
17:
18: @Override
19: protected void onCreate(Bundle savedInstanceState) {
20: super.onCreate(savedInstanceState);
21: setContentView(R.layout.activity_main);
22:
23: seekBar = (SeekBar) findViewById(R.id.seek_bar);
24: textView = (TextView) findViewById(R.id.speed);
25:
26: //设定拖动条的最大值,其将为该拖动条显示的基数。
27: seekBar.setMax(100);
28: //该方法为 seekBar 注册一个监听, 当 SeekBar 发生改变时调用参数 l 中的对应方法。
29: seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
30:
31: @Override
32: public void onStopTrackingTouch(SeekBar seekBar) {
33: // TODO Auto-generated method stub
34: //设置标记需要刷新
35: flag = true;
36: refrech();
37:
38: }
39:
40: @Override
41: public void onStartTrackingTouch(SeekBar seekBar) {
42: // TODO Auto-generated method stub
43: flag = false;
44: }
45:
46: @Override
47: public void onProgressChanged(SeekBar seekBar, int progress,
48: boolean fromUser) {
49: // TODO Auto-generated method stub
50: //更改textView的内容
51: textView.setText("进度为: " + progress + "%");
52: }
53: });
54:
55:
56: }
57:
58: protected void refrech() {
59: // TODO Auto-generated method stub
60: new Thread(new Runnable() {
61:
62: @Override
63: public void run() {
64: // TODO Auto-generated method stub
65: while(flag && seekBar.getProgress()<100)
66: {
67: try{
68: //暂停一秒
69: Thread.sleep(1000);
70: }
71: catch (Exception e)
72: {
73: // TODO: handle exception
74: e.printStackTrace();
75: }
76: //将一个 Runnable 对象添加到消息队列当中,
77: //并且当执行到该对象时执行 run()方法
78: handler.post(new Runnable() {
79: public void run() {
80: //重新设置进度条当前的值,加 1
81: seekBar.setProgress(seekBar.getProgress() + 1);
82: }
83: });
84:
85: }
86: }
87: }).start();
88: }
89:
90: @Override
91: public boolean onCreateOptionsMenu(Menu menu) {
92: // Inflate the menu; this adds items to the action bar if it is present.
93: getMenuInflater().inflate(R.menu.main, menu);
94: return true;
95: }
96:
97: }
说明:
当 SeekBarActivity 创建时,为 seekBar 注册监听事件。 并且调用 refresh()方法,
自动刷新拖动条的进度值。当拖动条进度值改变时,修改 seekBar 上部 TextView 的内容。