Android應用開發學習之進度條

來源:互聯網
上載者:User

進度條ProgressBar是一個經常用到的組件,它的使用也很簡單,只需要把進度條顯示在前台,然後在後台啟動一個線程,根據需要修改進度條的狀態。我們來看一個例子,該程式運行效果如所示:

 

該程式主布局檔案main.xml內容如下:
 

<?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="wrap_content"  android:layout_height="wrap_content"/>        <ProgressBar         android:id="@+id/progressBar2"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  style="?android:attr/progressBarStyleLarge"/>        <ProgressBar         android:id="@+id/progressBar3"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  style="?android:attr/progressBarStyleSmall"/>        <ProgressBar         android:id="@+id/progressBar4"  android:layout_width="match_parent"  android:layout_height="wrap_content"  style="?android:attr/progressBarStyleHorizontal"  android:max="100"  android:progress="0"  android:secondaryProgress="70"/>        <ProgressBar    android:id="@+id/progressBar5"     style="@android:style/Widget.ProgressBar.Large"    android:layout_width="match_parent"     android:layout_height="wrap_content"/>        <ProgressBar    android:id="@+id/progressBar6"     style="@android:style/Widget.ProgressBar.Small"    android:layout_width="match_parent"     android:layout_height="wrap_content"/>        <ProgressBar    android:id="@+id/progressBar7"     android:max="100"    android:progress="0"    android:secondaryProgress="70"    style="@android:style/Widget.ProgressBar.Horizontal"    android:layout_width="match_parent"     android:layout_height="wrap_content"/></LinearLayout>

主Activity檔案內容如下:

package com.liuhaoyu;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.ProgressBar;import android.widget.Toast;public class MainActivity extends Activity {private ProgressBar pb1, pb2, pb3, pb4, pb5, pb6, pb7;private Handler PbHandler;private int progress = 0;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                pb1 = (ProgressBar)findViewById(R.id.progressBar1);        pb2 = (ProgressBar)findViewById(R.id.progressBar2);        pb3 = (ProgressBar)findViewById(R.id.progressBar3);        pb4 = (ProgressBar)findViewById(R.id.progressBar4);        pb5 = (ProgressBar)findViewById(R.id.progressBar5);        pb6 = (ProgressBar)findViewById(R.id.progressBar6);        pb7 = (ProgressBar)findViewById(R.id.progressBar7);                PbHandler = new Handler(){        public void handleMessage(Message msg){        if(msg.what == 0x0){        pb4.setProgress(progress);        pb7.setProgress(progress);        }        else        {        Toast.makeText(MainActivity.this, "任務執行成功!", Toast.LENGTH_SHORT).show();        pb1.setVisibility(View.GONE);        pb2.setVisibility(View.GONE);        pb3.setVisibility(View.GONE);        pb4.setVisibility(View.GONE);        pb5.setVisibility(View.GONE);        pb6.setVisibility(View.GONE);        pb7.setVisibility(View.GONE);        }        }        };                new Thread(new Runnable() {public void run() {while (true) {progress += Math.random()*10;try{Thread.sleep(200);}catch(InterruptedException e){e.printStackTrace();}Message m = new Message();if(progress < 100){m.what = 0x0;PbHandler.sendMessage(m);}else{m.what=0x1;PbHandler.sendMessage(m);break;}}}}).start();    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.