安卓进度条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 + "%");
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41282789/article/details/82658258