1. 程式人生 > >安卓進度條ProgressBar

安卓進度條ProgressBar

介面程式碼

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
android:layout_height="match_parent" tools:context=".MainActivity">
android:layout_width="match_parent" android:layout_height="match_parent"> <!-- proressbar常用屬性 max:進度條的最大值 progress:當前進度值 style:進度條樣式 ?android:attr/progressBarStyleHorizontal" 預設為圓形 注意:不能在主執行緒中執行耗時的操作,只能在子執行緒中操作 另外,在子執行緒中不能操作主執行緒中的控制元件(ProgressBar除外) -->
<ProgressBar android:id="@+id/main_pb" android:layout_width="match_parent" android:max="100" android:progress="0" style="?android:attr/progressBarStyleHorizontal" android:layout_height="40dp" /> <TextView android:id="@+id/main_tv" android:layout_width
="match_parent" android:textColor="@color/orange" android:layout_marginLeft="160dp" android:textSize="30dp" android:layout_height="wrap_content" />
<Button android:layout_width="100dp" android:layout_marginTop="50dp" android:text="下載" android:onClick="onBtn" android:layout_height="40dp" /> </FrameLayout>

後臺程式碼

package com.example.app6;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private ProgressBar progressBar;
    private int progress=0;
    private TextView textView;
    private MyHandler handler =new MyHandler( );
    int code =1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar=findViewById(R.id.main_pb);
        textView=findViewById(R.id.main_tv);
    }

    public void onBtn(View view) {
        Toast toast =Toast.makeText(this,"開始下載",Toast.LENGTH_SHORT);
        if(0==progress){
            new MyThread().start();
            toast.show();
        }
        else{
            toast.setText("正在下載");
            toast.show();
        }
    }
    private class  MyThread extends  Thread{
        @Override
        public void run() {
            super.run();
            while(true){
                try {
                    Thread.sleep(100);
                    if(100==progress){
                        progress=0;
                        break;
                    }
                    Message msg =new Message();
                    msg.what=1;
                    handler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    private class  MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(code==msg.what) {
                progress++;
                progressBar.setProgress(progress);
                textView.setText(progress + "%");
            }
        }
    }
}