Android ProgressDialog弹框的两种用法?

导读:本篇文章讲解 Android ProgressDialog弹框的两种用法?,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一、效果:

Android ProgressDialog弹框的两种用法?      Android ProgressDialog弹框的两种用法?

二、(进度条风格,风格为圆形,旋转的)和(进度条风格,风格为长形)

1.XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆形进度条" />
    <Button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="长形进度条" />
</LinearLayout>

2.在Activity中使用

package com.ruidde.pictureselectordemo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Context mContext;
    private Button Button1 = null;
    private Button Button2 = null;
    private int count = 0;
    //声明进度条对话框
    private ProgressDialog progressDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        initData();
    }

    private void initData() {
        Button1 = findViewById(R.id.Button01);
        Button2 = findViewById(R.id.Button02);

        Button1.setOnClickListener(new Button1Listener());
        Button2.setOnClickListener(new Button2Listener());
    }

    private class Button1Listener implements View.OnClickListener {

        public void onClick(View v) {
            //创建ProgressDialog对象
            progressDialog = new ProgressDialog(MainActivity.this);
            // 设置进度条风格,风格为圆形,旋转的
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            // 设置ProgressDialog 标题
            progressDialog.setTitle("提示");
            // 设置ProgressDialog 提示信息
            progressDialog.setMessage("这是一个圆形进度条对话框");
            // 设置ProgressDialog 标题图标
            progressDialog.setIcon(R.mipmap.ic_launcher);
            // 设置ProgressDialog 的进度条是否不明确
            progressDialog.setIndeterminate(false);
            // 设置ProgressDialog 是否可以按退回按键取消
            progressDialog.setCancelable(true);
            //设置ProgressDialog 的一个Button
            progressDialog.setButton("确定", new SureButtonListener());
            // 让ProgressDialog显示
            progressDialog.show();
        }
    }

    //Dialog中确定按钮的监听器
    private class SureButtonListener implements android.content.DialogInterface.OnClickListener {

        public void onClick(DialogInterface dialog, int which) {
            //点击“确定按钮”取消对话框
            Toast.makeText(mContext ,"确定按钮" ,Toast.LENGTH_SHORT).show();
            dialog.cancel();
        }

    }

    private class Button2Listener implements View.OnClickListener {

        public void onClick(View v) {
            count = 0;
            // 创建ProgressDialog对象
            progressDialog = new ProgressDialog(MainActivity.this);
            // 设置进度条风格,风格为长形
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            // 设置ProgressDialog 标题
            progressDialog.setTitle("提示");
            // 设置ProgressDialog 提示信息
            progressDialog.setMessage("这是一个长形对话框进度条");
            // 设置ProgressDialog 标题图标
            progressDialog.setIcon(R.mipmap.ic_launcher);
            // 设置ProgressDialog 进度条进度
            progressDialog.setProgress(100);
            // 设置ProgressDialog 的进度条是否不明确
            progressDialog.setIndeterminate(false);
            // 设置ProgressDialog 是否可以按退回按键取消
            progressDialog.setCancelable(true);
            // 让ProgressDialog显示
            progressDialog.show();
            new Thread() {
                public void run() {
                    try {
                        while (count <= 100) {
                            // 由线程来控制进度。
                            progressDialog.setProgress(count);
                            count = count + 2;
                            Thread.sleep(100);
                        }
                        progressDialog.cancel();
                    } catch (InterruptedException e) {
                        progressDialog.cancel();
                    }
                }
            }.start();
        }
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119209.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!