Android的Thread & ProgressBar
16lz
2021-12-04
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressbar_default"
/>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressbar_Horizontal"
android:max="100"
/>
</LinearLayout>
//-----------------------soruce code--------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressbar_default"
/>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressbar_Horizontal"
android:max="100"
/>
</LinearLayout>
//-----------------------soruce code--------------------------------
package android.demo.thread0;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.widget.ProgressBar;public class Thread0_Activity extends Activity {ProgressBar bar0;ProgressBar bar1;boolean isRunning=false;Handler handler=new Handler() { public void handleMessage(Message msg)//會從Message Queue取出Message { bar0.incrementProgressBy(5); bar1.incrementProgressBy(5); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bar0 = (ProgressBar)findViewById(R.id.progressbar_Horizontal); bar1 = (ProgressBar)findViewById(R.id.progressbar_default); } @Override public void onStart() { super.onStart(); bar0.setProgress(0); bar1.setProgress(0); //Thread background=new Thread(run0); // background.start(); ProgressThread ProgressThread0=new ProgressThread(); ProgressThread0.start(); } privateRunnable run0=new Runnable(){ @Override public void run() { // TODO Auto-generated method stub try { for (int i=0;i<20 /*&& isRunning*/;i++) { Thread.sleep(1000); handler.sendMessage(handler.obtainMessage()); } } catch (Throwable t) { // just end the background thread } }};public class ProgressThread extends Thread{public void run(){ try { for (int i=0;i<20 /*&& isRunning*/;i++) { sleep(1000); handler.sendMessage(handler.obtainMessage()); } } catch (Throwable t) { // just end the background thread }}};}
更多相关文章
- [android] android framework中的 PhoneNumberUtils 类详解
- Android(安卓)Handler机制8之消息的取出与消息的其他操作
- 将数组中的值取出并重新排序
- 如何提取视频声音,将视频中的音乐提取出来
- yum仓库与sed一般应用
- Android(安卓)JNI编程学习
- [转]关于读取手机号码
- libxml2 的一个简单测试示例
- ClassCastException: java.util.HashSet cannot be cast to java