【Android】6.3 ProgressDialog

本文介绍Android应用中进度条对话框(ProgressDialog)的使用方法,包括条形进度条和圆形进度条的实现方式。通过示例展示了如何在应用程序中创建并控制这两种进度条。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分类: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();
            });
        }
    }
}

运行。

转载于:https://www.cnblogs.com/rainmj/p/5185202.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值