欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

《Android 进度条》

程序员文章站 2024-01-01 11:43:43
...

1,res/layout/ 代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_jdt_tv"   //ID
        android:text="0%"   //初始值
        android:textSize="30dp"
        android:textColor="#32cd32"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="170dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:id="@+id/pb_jdt_jdt"   //进度条ID
        android:max="100"   //进度条最大值
        android:progress="0"   //进度条初始值
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="21dp" />

    <Button
        android:id="@+id/bt_jdt_ks"   //按钮ID
        android:text="开始"
        android:onClick="ks"   //触发事件
        android:background="#1e90ff"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="42dp" />

</LinearLayout>

2,java/ 代码

package com.example.a1.android06;

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

public class MainActivity extends AppCompatActivity {

    private ProgressBar pb_jdt_jdt;
    private TextView tv_jdt_tv;
    private int ProgressCode=0;
    private Handler MyHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                ProgressCode++;
                pb_jdt_jdt.setProgress(ProgressCode);
                tv_jdt_tv.setText(ProgressCode+"%");
                break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jdt);
        tv_jdt_tv=findViewById(R.id.tv_jdt_tv);
        pb_jdt_jdt=findViewById(R.id.pb_jdt_jdt);
        pb_jdt_jdt.setProgress(ProgressCode);
    }

    public void ks(View view) {
        if (ProgressCode==0){
            new MyThread().start();
        }
    }

    class MyThread extends Thread{

        public void run(){
            super.run();

            while (true){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if(ProgressCode==100){
                    ProgressCode=0;
                    break;
                }

                Message msg=new Message();
                msg.what=1;
                MyHandler.sendMessage(msg);

            }

        }

    }



}

上一篇:

下一篇: