Android 应用、驱动开发(十九)Android studio ProgressDialog应用

一、运行效果:

二、MainActivity.java:

package com.example.a027_progressdialogapp;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button button,button2;
    private ProgressDialog progressDialog,progressDialog1;

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

        button = findViewById(R.id.button);
        button2 = findViewById(R.id.button2);

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                progressDialog1 = new ProgressDialog(MainActivity.this);
                progressDialog1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                progressDialog1.setCancelable(false);
                progressDialog1.setIcon(R.drawable.as8);
                progressDialog1.setTitle("hello");
                progressDialog1.setMax(100);
//                progressDialog1.setButton(DialogInterface.BUTTON_POSITIVE,"确定",
//                    new DialogInterface.OnClickListener(){
//                        @Override
//                        public void onClick(DialogInterface progressDialog, int which) {
//
//                        }
//                    });
                progressDialog1.setButton(DialogInterface.BUTTON_NEGATIVE,"取消",
                    new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface progressDialog, int which) {

                        }
                    });
                progressDialog1.setMessage("加载中...");
                progressDialog1.show();
            }
        });


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setCancelable(false);
                progressDialog.setIcon(R.drawable.as7);
                progressDialog.setTitle("提示");
                progressDialog.setMax(6000);
//                progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定",
//                    new DialogInterface.OnClickListener(){
//                        @Override
//                        public void onClick(DialogInterface progressDialog, int which) {
//
//                        }
//                    });
//                progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消",
//                    new DialogInterface.OnClickListener(){
//                        @Override
//                        public void onClick(DialogInterface progressDialog, int which) {
//
//                        }
//                    });
                progressDialog.setMessage("装载中...");
                progressDialog.show();
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            for(int i = 0;i < progressDialog.getMax();i += 100){
                                progressDialog.setProgress(i);
                                Thread.sleep(300);
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        progressDialog.dismiss();

                    }
                }).start();

            }
        });
    }

}

三、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_margin="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="水平进度条" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="圆形进度条"
            android:layout_margin="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

四、工程源码:

androidstudio开发app项目027-ProgressDialogApp.rar资源-CSDN文库

相关推荐

  1. Android Gradle 开发应用 (): Gradle 脚本最佳实践

    2024-06-18 17:26:01       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-18 17:26:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-18 17:26:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-18 17:26:01       82 阅读
  4. Python语言-面向对象

    2024-06-18 17:26:01       91 阅读

热门阅读

  1. 【网络协议栈】TCP/IP相关知识点收集

    2024-06-18 17:26:01       28 阅读
  2. 0, GPU Microarchitecture介绍

    2024-06-18 17:26:01       31 阅读
  3. Vue 3 的 Teleport 组件实现跨层级通信

    2024-06-18 17:26:01       36 阅读
  4. 萤石视频接入api接口示例

    2024-06-18 17:26:01       34 阅读
  5. 速盾:高防服务器防御 DDoS 攻击的掩护技巧

    2024-06-18 17:26:01       30 阅读
  6. stringstream的使用

    2024-06-18 17:26:01       33 阅读
  7. Spring Boot常用注解

    2024-06-18 17:26:01       32 阅读
  8. 近期学习文章

    2024-06-18 17:26:01       31 阅读
  9. 2024.06.05校招 实习 内推 面经

    2024-06-18 17:26:01       32 阅读