Android-Handler更新View加线程
代码
package net.blogjava.mobile; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.ProgressBar; public class Main extends Activity { private ProgressBar progressBar1; private ProgressBar progressBar2; private Handler handler = new Handler(); private int count1 = 0; private int count2 = 0; private Runnable doUpdateProgressBar1 = new Runnable() { @Override public void run() { for (count1 = 0; count1 <= progressBar1.getMax(); count1++) { handler.post(new Runnable() { @Override public void run() { progressBar1.setProgress(count1); } }); } } }; private Runnable doUpdateProgressBar2 = new Runnable() { @Override public void run() { for (count2 = 0; count2 <= progressBar2.getMax(); count2++) { handler.post(new Runnable() { @Override public void run() { progressBar2.setProgress(count2); } }); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progressBar1 = (ProgressBar) findViewById(R.id.progressbar1); progressBar2 = (ProgressBar) findViewById(R.id.progressbar2); Thread thread1 = new Thread(doUpdateProgressBar1, "thread1"); thread1.start(); Thread thread2 = new Thread(doUpdateProgressBar2, "thread2"); thread2.start(); } }
布局
<?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"> <ProgressBar android:id="@+id/progressbar1" android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="20000" android:layout_marginTop="10dp" /> <ProgressBar android:id="@+id/progressbar2" android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="10000" android:layout_marginTop="10dp" /> </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。