作者:堕落的天使

图片效果

模拟下载的进度条ProgressBar-LMLPHP模拟下载的进度条ProgressBar-LMLPHP

activity_main.xml(代码)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="68dp"
android:layout_marginTop="32dp"
android:layout_toLeftOf="@+id/textView1" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/progressBar1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="15dp"
android:layout_marginTop="22dp"
android:text="开始下载" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar1"
android:text="0%" /> </RelativeLayout>

MainActivity.java(代码)

package com.example.progresstest;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView; public class MainActivity extends Activity { private TextView text;
private Button button; private int max;
private int progressStatus = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final ProgressBar prog = (ProgressBar) findViewById(R.id.progressBar1);
prog.setMax(100);
max = prog.getMax();
text = (TextView) findViewById(R.id.textView1);
button = (Button) findViewById(R.id.button1); final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
progressStatus = msg.what;
prog.setProgress(progressStatus);
text.setText(msg.what+"%");
}
};
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
new Thread(new Runnable() { @Override
public void run() {
for(int i=0; i <= max; i++){
Message msg = handler.obtainMessage();//重点
msg.what= i;
handler.sendMessage(msg);
try{
Thread.sleep(100);
}catch(Exception e){
e.printStackTrace();
}
} }
}).start();
}
}); }

重点:1.handler的作用:

模拟下载的进度条ProgressBar-LMLPHP

   2.Message:尽管Message的构造器是公开的,但是获取Message对象的最好方法是调用Message.obtain()或者Handler.obtainMessage(), 这样是从一个可回收对象池中获取Message对象。

推荐链接:http://www.cnblogs.com/wt616/archive/2011/06/21/2086162.html

04-30 06:33