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;
}