Android 控件ProgressBar进度条

                             Android 控件---ProgressBar进度条

下面详细介绍ProgressBar

一、说明

  在某些操作的进度中的可视指示器,为用户呈现操作的进度,视频一般都有进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度。

二、常用属性

    style="?android:attr/progressBarStyle"     默认为圆形

    style="?android:attr/progressBarStyleHorizontal"    水平样式

              android:max="100"    设置最大值

              android:progress="33"   设置当前进度

三、重要方法

    getMax():返回这个进度条的范围的上限

    getProgress():返回进度

    getSecondaryProgress():返回次要进度

    incrementProgressBy(int diff):指定增加的进度

    isIndeterminate():指示进度条是否在不确定模式下

    setIndeterminate(boolean indeterminate):设置不确定模式下

    setVisibility(int v):设置该进度条是否可视

         注意:1、给线程设置休眠会出现错误     
                   Thread.sleep(100);//抛异常           SystemClock.sleep(100);//不会抛异常

                   2、不能在主线程中执行耗时的操作,只能在子线程中操作 ,             

                        另外,在子线程中不能操作主线程中的控件 (ProgressBar除外)

 

四、重要事件

    onSizeChanged(int w, int h, int oldw, int oldh):当进度值改变时引发此事件

五、实例

1.布局文件 (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:id="@+id/tv_main_tv1"
           android:layout_width="60dp"
           android:layout_height="match_parent" />
        <ProgressBar
            android:id="@+id/pb_main_pb1"
            android:layout_width="match_parent"
            style="?android:attr/progressBarStyleHorizontal"
            android:max="100"
            android:layout_height="60dp" />
   </FrameLayout>

    <Button
        android:id="@+id/btn_main_btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="setProcess"/>

</LinearLayout>

2、代码文件(MainActivity.java代码)

package com.example.a0911_homework_05;


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

public class MainActivity extends AppCompatActivity {

   //初始化控件
    private ProgressBar pb_main_pb1;
    private int progress;
    private TextView tv_main_tv1;
    private myHandler myHandler = new myHandler();

    private int code = 1;//代表哪一部视频
    private class myHandler extends Handler{//通过Handler给主线程发送信息
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(code == msg.what){
                progress++;//每执行一遍就加1
                pb_main_pb1.setProgress(progress);
                tv_main_tv1.setText(progress + "%");//给百分比设置值
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb_main_pb1 = this.findViewById(R.id.pb_main_pb1);//获取进度条控件
        tv_main_tv1 = this.findViewById(R.id.tv_main_tv1);
    }


    public void setProgress(View view) {//点击就开始下载
        if(0 == progress){//如果进度为0
            new MyThread().start();//开始线程
        }
    }

    private class MyThread extends Thread{//子线程
        @Override
        public void run() {//重写线程
            super.run();
            while(true){
                try {
                    Thread.sleep(100);//给线程设置睡眠时间0.1s
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(progress == 100){//如果进度为100就终止
                    progress = 0;//恢复默认值0
                    break;//结束终止
                }
                //触发handleMessage方法
                Message msg = new Message();
                msg.what = 1;//给线程做标识
                myHandler.sendMessage(msg);

            }
        }
    }



}

3:线程小结
SubThread->MainThread 错误
SubThread->Handler->MainThread 正确

上面的为进度条,下面灰色条为按钮,点击按钮进度条就会运行

猜你喜欢

转载自blog.csdn.net/cms18374672699/article/details/82659085