android(ProgressDialog)

1.进度条的显示

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=".MainActivity">

   <Button
       android:id="@+id/button1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="圆形的进度条"/>


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆形的进度条"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆形的进度条"/>
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆形的进度条"/>
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆形的进度条"/>
    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="长方形的进度条"/>
</LinearLayout>

2.我的MainActivity代码

package com.example.android012;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private Button button6;

    private  int M_count;  //这是进度条的一个刻度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1=findViewById(R.id.button1);
        button1.setOnClickListener(this);

        button2=findViewById(R.id.button2);
        button2.setOnClickListener(this);

        button3=findViewById(R.id.button3);
        button3.setOnClickListener(this);

        button4=findViewById(R.id.button4);
        button4.setOnClickListener(this);

        button5=findViewById(R.id.button5);
        button5.setOnClickListener(this);

        button6=findViewById(R.id.button6);
        button6.setOnClickListener(this);
    }


    //几种实现方法
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:
                //弹出来之后,不能使用返回键取消
                ProgressDialog progressDialog=ProgressDialog.show(MainActivity.this,"请稍后......" ,"数据加载中");
                progressDialog.setCancelable(true);   //设置弹出进度条可以使用任意键关闭
                break;

            case R.id.button2:
                ProgressDialog dialog=new ProgressDialog(this);
                dialog.show();

                break;
            case R.id.button3:
               ProgressDialog p3=ProgressDialog.show(this,"请稍等","数据加载中......",false);
               p3.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  //样式
                break;
            case R.id.button4:
                ProgressDialog p4=ProgressDialog.show(this, "请稍等", "数据加载中......", false, true, new DialogInterface.OnCancelListener() {
                    @SuppressLint("WrongConstant")
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        Toast.makeText(MainActivity.this,"点了取消",0).show();
                    }
                });


                break;

            case R.id.button5:
                createDialog_2();
                break;


            case R.id.button6:
                createDialog_3();
                break;
        }

    }
    private void createDialog_2(){
        ProgressDialog dialog=new ProgressDialog(this);
        //设置进度条的风格
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //设置进度条的标题
        dialog.setTitle("提示");
        //设置进度条的提示信息
        dialog.setMessage("这是一个圆形的进度条");
        //设置标题的图片
        dialog.setIcon(R.drawable.ic_launcher_foreground);
        //设置进度条是否明确
        dialog.setIndeterminate(false);
        //设置是否可以使用返回键关闭
        dialog.setCancelable(true);
        //设置一个确定按钮
        dialog.setButton(ProgressDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"点了确定",0).show();
            }
        });

        dialog.show();
    }

    private void createDialog_3(){
        final ProgressDialog dialog=new ProgressDialog(this);
        //设置进度条的风格
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //设置进度条的标题
        dialog.setTitle("提示");
        //设置进度条的提示信息
        dialog.setMessage("这是一个长方形的进度条");
        //设置标题的图片
        dialog.setIcon(R.drawable.ic_launcher_foreground);
        //设置进度条是否明确
        dialog.setIndeterminate(false);
        //设置是否可以使用返回键关闭
        dialog.setCancelable(true);
        //设置一个确定按钮
        dialog.setButton(ProgressDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"点了确定",0).show();
            }
        });
        dialog.show();
        //使用线程处理
        new Thread(){   //匿名对象
            //重新run()方法


            @Override
            public void run() {
                try {
                    while (M_count<=100){
                      dialog.setProgress(M_count);
                      Thread.sleep(100);
                      M_count++;
                    }
                    //关闭进度条
                    dialog.cancel();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }
}

实现效果
在这里插入图片描述

发布了60 篇原创文章 · 获赞 3 · 访问量 2184

猜你喜欢

转载自blog.csdn.net/ysy_1_2/article/details/104439882