【Android】6.3 ProgressDialog

分类:C#、Android、VS2015;

创建日期:2016-02-08

一、简介

进度条对话框(ProgressDialog)常用于不能在短时间内快速完成的操作,显示进度条的目的是为了让用户明白程序正在处理的进度,避免用户感觉莫名其妙。

本示例演示了两种进度条的基本用法:条形进度条和圆形进度条。

二、示例==Demo03ProgressDialog

1、运行截图

image  image

2、添加Demo03_ProgressDialog.axml文件

在layout文件夹下添加该文件。

<?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/btnPprogress"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="条形进度条" />
    <Button
        android:id="@+id/btnCircle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="圆形进度条" />
</LinearLayout>

3、添加Demo03ProgressDialog.cs文件

在SrcActivity文件夹下添加该文件。

using System;
using Android.App;
using Android.OS;
using Android.Widget;

namespace ch06demos.SrcActivity
{
    [Activity(Label = "Demo03ProgressDialog")]
    public class Demo03ProgressDialog : Activity
    {
        private int progress;
        private ProgressDialog dialog;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Demo03_ProgressDialog);
            var btnProgress = FindViewById<Button>(Resource.Id.btnPprogress);
            btnProgress.Click += BtnProgress_Click;
            var btnCircle = FindViewById<Button>(Resource.Id.btnCircle);
            btnCircle.Click += BtnCircle_Click;
        }

        private void BtnProgress_Click(object sender, EventArgs e)
        {
            progress = 0;
            dialog = new ProgressDialog(this)
            {
                Progress = progress,
                Indeterminate = false,
            };
            dialog.SetProgressStyle(ProgressDialogStyle.Horizontal);
            dialog.SetTitle("条形进度条示例");
            dialog.SetMessage("正在下载……");
            dialog.SetIcon(Resource.Drawable.Icon);
            dialog.SetCancelable(true);
            dialog.SetButton("取消", delegate
            {
                Toast.MakeText(this, "已取消下载!", ToastLength.Long).Show();
            });
            dialog.Show();
            RunTask(showProgress: false);
        }

        private void BtnCircle_Click(object sender, EventArgs e)
        {
            progress = 0;
            dialog = new ProgressDialog(this)
            {
                Progress = progress,
                Indeterminate = false
            };
            dialog.SetProgressStyle(ProgressDialogStyle.Spinner);
            dialog.SetTitle("环转圆形进度条示例");
            dialog.SetMessage("正在下载……");
            dialog.SetIcon(Resource.Drawable.Icon);
            dialog.SetCancelable(true);
            dialog.SetButton("取消", delegate
            {
                Toast.MakeText(this, "已取消下载!", ToastLength.Long).Show();
            });
            dialog.Show();
            RunTask(showProgress: true);
        }

        //模拟长时间执行的任务
        private void RunTask(bool showProgress)
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                while (progress < 100)
                {
                    dialog.Progress = progress++;
                    if (showProgress)
                    {
                        RunOnUiThread(() =>
                        {
                            dialog.SetMessage("正在下载……" + progress + "%");
                        });
                    }
                    System.Threading.Thread.Sleep(100);
                }
                dialog.Cancel();
            });
        }
    }
}

运行。

posted @ 2016-02-08 20:14  rainmj  阅读(640)  评论(0编辑  收藏  举报