Android Dialog、AlertDialog、ProgressDialog、SweetAlertDialog、CustomDialog、DialogFragment

本文深入探讨了Android中对话框的多种实现方式,包括AlertDialog、ProgressDialog、SweetAlertDialog、自定义Dialog和DialogFragment,提供了丰富的代码示例和使用场景,是Android开发者不可多得的参考资料。

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

AlertDialog

在这里插入图片描述

    private void myAlertDialog() {
        new AlertDialog
                .Builder(activity)
                .setMessage("关闭当前页面..")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        activity.finish();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //doNothing
                    }
                })
                .setCancelable(false)//只能通过响应dialog的事件来关闭dialog
                .show();
    }

ProgressDialog

在这里插入图片描述

 package com.zhangyu.myopengl.dialog;

import android.app.Activity;
import android.app.ProgressDialog;

import java.lang.ref.WeakReference;

public class MyProgressDialog {

    private WeakReference<Activity> activityWeakReference;

    public MyProgressDialog(WeakReference<Activity> activityWeakReference) {
        this.activityWeakReference = activityWeakReference;
    }

    private ProgressDialog progressDialog;

    //显示弹窗
    public void showProgress() {
        showProgress("");
    }

    //显示弹窗
    public void showProgress(String msg) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(activityWeakReference.get());
            progressDialog.setCancelable(false);
        }
        if (activityWeakReference.get() != null && !activityWeakReference.get().isFinishing()) {
            progressDialog.setTitle(msg);
            progressDialog.show();
        }
    }

    //取消弹窗
    public void dismissProgress() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

}

SweetAlertDialog

教程
https://blog.youkuaiyun.com/taa1007/article/details/79551928)
GitHub:
https://github.com/pedant/sweet-alert-dialog
在这里插入图片描述

    private Dialog dialog;

    public void showProgress(float progress) {
        if (dialog == null) {
            dialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
            dialog.setCancelable(false);
        }
        dialog.setTitle((int) (progress * 100) + "%");
        dialog.show();
    }

CustomDialog(自定义dialog)

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.maka.opencut.R;

public class DialogProgress {

    private Context context;
    private Dialog dialog;

    public DialogProgress(Context context) {
        this.context = context;
    }

    public void showDialog() {
        if (dialog == null) {
            View view = LayoutInflater.from(context)
                    .inflate(R.layout.dialog_grab_cut_progress, null);
            ImageView iv_loading = view.findViewById(R.id.iv_loading);
            animLoading(iv_loading);
            dialog = new Dialog(context);
            dialog.setCancelable(false);
            dialog.setContentView(view);
        }
        dialog.show();
    }

    private void animLoading(View v) {
        ObjectAnimator rotation = ObjectAnimator.ofFloat(v, "rotation", 0, 360);
        rotation.setRepeatCount(-1);
        rotation.setDuration(1000);
        rotation.start();
    }


    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

}

DialogFragment(系统推荐的方法)

    private void initDialog() {
        MyDialogFragment myDialogFragment = MyDialogFragment.newInstance("canshu","123");
        myDialogFragment.show(getSupportFragmentManager(),"MyDialogFragment");
    }
public class MyDialogFragment extends DialogFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public MyDialogFragment() {
        // Required empty public constructor
    }

    public static MyDialogFragment newInstance(String param1, String param2) {
        MyDialogFragment fragment = new MyDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_dialog, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        TextView tvTitle = view.findViewById(R.id.tv_title);
        tvTitle.setText(mParam1 + "\n" + mParam2);
    }
    
}
  • DialogFragment传递参数的另一种方式,重写show方法
   
    public void show(@NonNull FragmentManager manager, @Nullable String tag, String userName, String industry) {
        super.show(manager, tag);
        this.userName = userName;
        this.industry = industry;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值