2.14 ProgressDialog 进度条对话框的使用

一、布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.yuancan.test102301.MainActivity">

    <Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1、普通的圆形进度条对话框"
        android:layout_marginTop="20dp"
        />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="2、条形进度条"
        android:layout_marginTop="70dp"
        />

    <Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="120dp"
        android:text="3、更新条形进度条"
        />

</RelativeLayout>

二、Java代码

package com.example.yuancan.test102301;

import android.app.ProgressDialog;
import android.content.Context;
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;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_one;
    private Button btn_two;
    private Button btn_three;
    private ProgressDialog pd1;
    private ProgressDialog pd2;
    private final static int MAXVALUE = 100;
    private int progressStart = 0;
    private int add = 0;
    private Context mContext;

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

        mContext = MainActivity.this;
        btn_one = (Button)findViewById(R.id.btn_1);
        btn_two = (Button)findViewById(R.id.btn_2);
        btn_three = (Button)findViewById(R.id.btn_3);
        btn_one.setOnClickListener(this);
        btn_two.setOnClickListener(this);
        btn_three.setOnClickListener(this);
    }

    final Handler hand = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 123) {
                pd2.setProgress(progressStart);
            }
            if (progressStart >= MAXVALUE) {
                pd2.dismiss();
            }
        }
    };


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_1:
            ProgressDialog.show(MainActivity.this,"资源加载中","资源加载中,请稍后...",false,true);
            break;

            case R.id.btn_2:
                pd1 = new ProgressDialog(mContext);
                pd1.setTitle("软件更新中");
                pd1.setMessage("软件正在更新,请稍后...");
                pd1.setCancelable(true);
                pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pd1.setIndeterminate(true);
                pd1.show();
                break;

            case R.id.btn_3:
                progressStart = 0;
                add = 0;
                pd2 = new ProgressDialog(MainActivity.this);
                pd2.setMax(MAXVALUE);
                pd2.setTitle("文件读取中");
                pd2.setMessage("文件加载中,请稍后...");
                pd2.setCancelable(false);
                pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pd2.setIndeterminate(false);
                pd2.show();

                new Thread() {
                    public void run() {
                        while (progressStart < MAXVALUE) {
                            progressStart = 2 * usetime();
                            hand.sendEmptyMessage(123);
                        }
                    }
                }.start();
                break;
        }
    }
    public int usetime() {
        add++;
        try {
            Thread.sleep(100);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        return add;
    }
}

这里写图片描述

这里写图片描述

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值