android各种提示Dialog 弹出框

2023-02-10 19:00:13 浏览数 (1)

开发过程中 经常需要各种弹出框 用来做提示  或者 展示一些 数据信息。写了一个 DialogUtil , 话不多说 直接上代码 ,希望对大家有帮助。

代码语言:javascript复制
public static Dialog getProcessDialog(Activity activity, CharSequence message,
        boolean dismissTouchOutside, boolean cancelable) {
    final LayoutInflater inflater = LayoutInflater.from(activity);
    View v = inflater.inflate(R.layout.progress_hud, null);
    Dialog dialog = getCustomDialog(activity, v, dismissTouchOutside, cancelable, -1);
    if (dismissTouchOutside) {
        dialog.setCanceledOnTouchOutside(true);
    } else {
        dialog.setCanceledOnTouchOutside(false);
    }


    ImageView spinner = (ImageView) v.findViewById(R.id.spinnerImageView);
    ((AnimationDrawable) spinner.getBackground()).start();
    TextView messageTv = (TextView) v.findViewById(R.id.message);
    if (TextUtils.isEmpty(message)) {
        messageTv.setVisibility(View.GONE);
    } else {
        messageTv.setText(message);
        messageTv.setVisibility(View.VISIBLE);
    }


    return dialog;
}


public static Dialog getCustomDialog(final Activity activity, View view, boolean dismissTouchOutside,
        boolean cancelable, int theme) {
    //Dialog dialog = theme > 0 ? new FullScreenDialogHUD(activity, theme)
    //        : new Dialog(activity, R.style.Dialog_FullScreen);
    Dialog dialog = new Dialog(activity, R.style.Dialog_FullScreen);
    dialog.setContentView(view);
    dialog.setCancelable(cancelable);
    dialog.setCanceledOnTouchOutside(dismissTouchOutside);
    if (!cancelable) {
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
                    dialog.dismiss();
                    activity.finish();
                }
                return false;
            }
        });
    }
    return dialog;
}


public static Dialog getOneButtonDialog(final Activity activity, String content,
        boolean dismissTouchOutside, boolean cancelable,
        DialogInterface.OnClickListener confirmOnClickListener) {
    Dialog dialog = new AlertDialog.Builder(activity)
            .setPositiveButton(R.string.confirm, confirmOnClickListener)
            .setCancelable(cancelable)
            .setMessage(content)
            .create();
    dialog.setCanceledOnTouchOutside(dismissTouchOutside);
    if (!cancelable) {
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
                    dialog.dismiss();
                    activity.finish();
                }
                return false;
            }
        });
    }
    return dialog;
}


public static Dialog getButtonsDialog(Activity activity, int resId,
        DialogInterface.OnClickListener confirmOnClickListener) {
    return getButtonsDialog(activity, activity.getString(resId), true, true, confirmOnClickListener,
            null);
}


public static Dialog getButtonsDialog(Activity activity, String content,
        DialogInterface.OnClickListener confirmOnClickListener) {
    return getButtonsDialog(activity, content, true, true, confirmOnClickListener, null);
}


public static Dialog getButtonsDialog(Activity activity, String content, boolean dismissTouchOutside,
        boolean cancelable, DialogInterface.OnClickListener confirmOnClickListener,
        DialogInterface.OnClickListener cancelOnClickListener) {
    Dialog dialog = new AlertDialog.Builder(activity)
            .setNegativeButton(R.string.cancel, cancelOnClickListener)
            .setPositiveButton(R.string.confirm, confirmOnClickListener)
            .setCancelable(cancelable)
            .setMessage(content)
            .create();
    dialog.setCanceledOnTouchOutside(dismissTouchOutside);
    return dialog;
}




progress_hud.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="@android:color/transparent">


    <ImageView
        android:id="@ id/spinnerImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@anim/spinner"/>


    <TextView
        android:id="@ id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textColor="#FFFFFF"/>


</LinearLayout>




style   Dialog_FullScreen
<style name="Dialog.FullScreen" parent="Theme.AppCompat.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@android:color/transparent</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">true</item>
</style>

有这些  其他弹出个人信息框 都可以自定义用getCustomDialog这个方法 传 对应的 view 实现。很方便。

希望对大家有帮助。

0 人点赞