Android的Thread & ProgressBar
16lz
2021-01-23
<?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 }}};}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer