实现简单的进度条(线程)

0,通过线程来改变进度条

在主线程中新建了一个线程,执行将数据写入数组的操作。当执行完成就改变一个statu,handler发送消息给looper,(mHandler.sendEmptyMessage(0X111);)
再然后在handler中处理这个msg。(public void handleMessage(@NonNull Message msg))
也就是改变进度条( activity.get().bar.setProgress(activity.get().status);)。

handler知识

为了避免内存泄漏,所以使用弱引用(大概)
弱引用

1,布局文件,上面的3个环形进度条是拿来看着玩的,可以随便删。


<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/Widget.ProgressBar.Large"
            />
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/Widget.Holo.ProgressBar.Small"/>

    LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="任务完成的进度"
        />
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:max="100"/>
LinearLayout>

2 ,接下来是主要的代码

package com.example.alertdialog;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;

import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity {
    private int[] data=new int[100];
    private int hasdata=0;
    int status=0;
    private ProgressBar bar;

    static class MyHandler extends Handler{
        private WeakReference<MainActivity> activity;
        MyHandler(WeakReference<MainActivity> activity){
            this.activity=activity;
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==0x111){
                activity.get().bar.setProgress(activity.get().status);
            }
        }

    }
    MyHandler mHandler  =new MyHandler(new WeakReference<>(this));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          bar = findViewById(R.id.bar);

          new Thread(){
            @Override
            public void run() {
                super.run();
                while (status < 100) {
                    try {
                        status = dowork();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mHandler.sendEmptyMessage(0X111);
                }
            }

          }.start();

    }
    public int dowork() throws InterruptedException {
        data[hasdata++]= (int)(Math.random()*100);
        Thread.sleep(1000);
        return hasdata;
    }
}

3

改进了一下,能够显示现在是第几次,和随机数


<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="任务完成的进度"
        />
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:max="10"/>

    <TextView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        />



LinearLayout>
package com.example.alertdialog;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity {
    private int[] data=new int[10];
    private int hasdata=0;
    int status=0;
    TextView text;
    private ProgressBar bar;

    static class MyHandler extends Handler{
        private WeakReference<MainActivity> activity;
        MyHandler(WeakReference<MainActivity> activity){
            this.activity=activity;
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==0x111){
                activity.get().bar.setProgress(activity.get().status);
                activity.get().text.setText("第"+activity.get().status+"次   随机数为"+activity.get().data[activity.get().status]);
            }
        }

    }
    MyHandler mHandler  =new MyHandler(new WeakReference<>(this));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          bar = findViewById(R.id.bar);
         text = findViewById(R.id.text);
        new Thread(){
            @Override
            public void run() {
                super.run();
                while (status < 10) {
                    try {
                        status = dowork();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mHandler.sendEmptyMessage(0X111);
                }
            }

          }.start();

    }
    public int dowork() throws InterruptedException {
        data[hasdata++]= (int)(Math.random()*100);
        Thread.sleep(1000);
        return hasdata;
    }



}


你可能感兴趣的:(Android,android,java,android,studio)