使用线程耗时操作来运行进度条

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_height="match_parent"
    tools:context="com.example.first.day_10_16.MainActivity">

<LinearLayout
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ProgressBar
        android:id="@+id/ProgressBar_1"
        android:layout_marginTop="100dp"
        android:progress="10"
        android:max="100"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/jindu"
        android:text="123123"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.first.day_10_16;

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.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    ProgressBar progressBar;
    TextView textView;
    Handler handler;
    Message message;
    int progress;

    int flag = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = this.findViewById(R.id.ProgressBar_1);
        textView = this.findViewById(R.id.jindu);
        final Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    message = new Message();
                    message.what = 10086;
                    if(progressBar.getProgress()<100) {
                        progress = (int) (Math.random() * 10);
                    }else{
                        flag = 1;
                        break;
                    }
                    try{
                        Thread.sleep(200);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    handler.sendMessage(message);
                }
            }
        });
        t.start();

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==10086){
                   progressBar.incrementProgressBy(progress);
                   textView.setText(""+progressBar.getProgress());
                }
                if(flag==1) {
                    progressBar.setVisibility(View.INVISIBLE);
                    textView.setVisibility(View.INVISIBLE);
                    Toast.makeText(MainActivity.this,"加载完成",Toast.LENGTH_SHORT).show();
                }
                super.handleMessage(msg);
            }
        };
    }
}

猜你喜欢

转载自blog.csdn.net/tdl081071tdy/article/details/83183260