标签:thread.sleep progressbar 进度条实时刷新
布局非常简单。
<LinearLayout 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:orientation="vertical"
tools:context="com.example.loadprogressbardemo.MainActivity" >
<ProgressBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载" />
</LinearLayout>
package com.example.loadprogressbardemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn;
private ProgressBar bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar)findViewById(R.id.bar);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.btn){
Thread t = new LoadThread();
t.start();
}
}
}
class LoadThread extends Thread{
public void run(){
for(int i=0;i<100;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bar.setProgress(bar.getProgress()+1);
}
}
}
}
标签:thread.sleep progressbar 进度条实时刷新
原文地址:http://blog.csdn.net/lisineng/article/details/44750773