025 Android 带进度条的对话框(ProgressDialog)

1.ProgressDialog介绍

ProgressDialog可以在当前界面弹出一个置顶于所有界面元素的对话框,同样具有屏蔽其他控件的交互能力,用于提示用户当前操作正在运行,让用户等待;

2.应用案例

(1)页面布局的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=".AdvanceToolActivity">

    <TextView
        style="@style/TitleStyle"
        android:text="高级工具" />

    <TextView
        android:id="@+id/tvAT_query_address"
        android:text="归属地查询"
        android:gravity="center"
        android:textSize="18dp"
        android:background="@drawable/selector_advanvetool_item_bg"
        android:drawableLeft="@android:drawable/btn_star"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"/>

    <TextView
        android:id="@+id/tvAT_sms_copy"
        android:text="短信备份"
        android:gravity="center"
        android:textSize="18dp"
        android:background="@drawable/selector_advanvetool_item_bg"
        android:drawableLeft="@android:drawable/btn_star"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"/>

</LinearLayout>

(2)java后台代码

package com.example.administrator.test62360safeguard;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
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.TextView;

public class AdvanceToolActivity extends AppCompatActivity {
    TextView tvAT_query_address;
    TextView tvAT_sms_copy;
    private final static int MAXVALUE = 100;
    private int currentProgress = 0;
    private ProgressDialog progressDialog;

    //更新UI界面
    @SuppressLint("HandlerLeak")
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            progressDialog.setProgress(currentProgress);
            if(currentProgress>=MAXVALUE){
                progressDialog.dismiss();//关闭进度条对话框
            }
        }
    };


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

        //电话归属地查询
        initPhoneAddress();
        //短信备份
        initSmsCopy();
    }

    /**
     * 短信备份
     */
    private void initSmsCopy() {
        tvAT_sms_copy=findViewById(R.id.tvAT_sms_copy);
        tvAT_sms_copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSmsCopyDialog();
            }
        });
    }

    /**
     * 显示一个带进度条的对话框
     */
    private void showSmsCopyDialog() {
        progressDialog = new ProgressDialog(this); //注意:这里的上下文必须是this,而不能用getApplicationContext()
        progressDialog.setIcon(R.mipmap.ic_launcher); //设置对话框的图标
        progressDialog.setTitle("短信备份");    //设置对话框标题
        progressDialog.setMax(MAXVALUE);        //设置进度条的最大值
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //指定进度条的样式为水平
        progressDialog.show();

        //开启一个线程
        new Thread(){
            @Override
            public void run() {
                while (currentProgress<MAXVALUE){
                    currentProgress=currentProgress+5;
                    try {
                        Thread.sleep(500); //模拟耗时操作
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler.sendEmptyMessage(0);
                }
            }
        }.start();
    }

    /**
     * 电话归属地查询
     */
    private void initPhoneAddress() {
        tvAT_query_address=findViewById(R.id.tvAT_query_address);
        tvAT_query_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),QueryAddressActivity.class);
                startActivity(intent);
            }
        });
    }
}

3.效果图

猜你喜欢

转载自www.cnblogs.com/luckyplj/p/10851364.html