Android中的常用控件之进度条(ProgressBar)

        ProgressBar的常用属性:style,进度条的样式,默认为圆形,用style="?android:attr/progressBarStyleHorizontal"可以将进度条设为条状;android:progress,进度条当前所处进度;android:max,进度条总进度。

        用线程实现进度条的注意事项:不能在主线程中执行耗时的操作,只能在子线程中操作;另外,在子线程中不能操作主线程中的控件(ProgressBar除外),错误示例(在子线程中操作主线程中的TextView)

activity_main.xml里的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<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">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="60dp">
        <TextView
            android:layout_width="80dp"
            android:textColor="@color/red"
            android:id="@+id/tv_main_desc"
            android:textSize="30dp"
            android:layout_height="match_parent" />

        <ProgressBar
            android:layout_width="match_parent"
            style="?android:attr/progressBarStyleHorizontal"
            android:id="@+id/pb_main_download"
            android:max="100"
            android:layout_height="match_parent" />

    </FrameLayout>


    <Button
        android:layout_width="match_parent"
        android:text="Download"
        android:onClick="download"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java里的代码如下:


public class MainActivity extends AppCompatActivity {

    private int p=0;//当前进度
    private ProgressBar pb_main_download;//进度条
    private TextView tv_main_desc;//显示文本的控件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //根据ID找到进度条
        pb_main_download=findViewById(R.id.pb_main_download);
    }
    
    //点击Download按钮所调用的方法
    public void download(View view) {
        if(0==p){//如果当前进度为0
            new myThread().start();//开启线程
        }
    }

    public class myThread extends Thread{
        @Override
        public void run() {
            super.run();
            while(true){
                try {
                    Thread.sleep(100);//使线程休眠0.1秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if(p==100){//当前进度等于总进度时退出循环
                    p=0;
                    break;
                }

                p++;
                pb_main_download.setProgress(p);//给进度条的当前进度赋值
                tv_main_desc.setText(p+"%");//显示当前进度为多少
            }
        }
    }
}

页面效果:

点击Download按钮后的效果:

解决方法:写一个类去继承Handler,重写handleMessage方法,Handler用于线程之间的通信,相当于一个中介。

修改后MainActivity.java里的代码如下:


public class MainActivity extends AppCompatActivity {

    private int p=0;//当前进度
    private ProgressBar pb_main_download;//进度条
    private TextView tv_main_desc;//显示文本的控件
    private MyHandler myHandler=new MyHandler();//新写的Handler类

    public class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int code=msg.what;//接受处理码
            switch (code){
                case 1:
                    p++;
                    pb_main_download.setProgress(p);//给进度条的当前进度赋值
                    tv_main_desc.setText(p+"%");//显示当前进度为多少
                    break;
            }
        }
    }

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

        //根据ID找到进度条
        pb_main_download=findViewById(R.id.pb_main_download);
        //根据ID找到显示文本的控件
        tv_main_desc=findViewById(R.id.tv_main_desc);
    }

    //点击Download按钮所调用的方法
    public void download(View view) {
        if(0==p){//如果当前进度为0
            new myThread().start();//开启线程
        }
    }

    public class myThread extends Thread{
        @Override
        public void run() {
            super.run();
            while(true){
                try {
                    Thread.sleep(100);//使线程休眠0.1秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if(p==100){//当前进度等于总进度时退出循环
                    p=0;
                    break;
                }

                Message msg=new Message();
                msg.what=1;
                myHandler.sendMessage(msg);//发送处理码
            }
        }
    }
}

点击Download按钮后的效果:

猜你喜欢

转载自blog.csdn.net/weixin_42687829/article/details/82728791