Android自定义Dialog网络加载等待弹窗

Android自定义Dialog网络加载等待弹窗_第1张图片  Android自定义Dialog网络加载等待弹窗_第2张图片

自定义的布局dialog_loading.xml




    

        

        
    

ProgressBar引用的drawable下的动画资源dialog_loading.xml

阿里巴巴矢量图标下载:https://www.iconfont.cn/

工具类

package com.shanjing.songsearch.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.shanjing.songsearch.R;

public class LoadingDialog extends Dialog {


    public LoadingDialog(Context context) {
        super(context);
    }

    public LoadingDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {

        private Context context;
        private String message;
        private boolean isShowMessage = true;
        private boolean isCancelable = false;
        private boolean isCancelOutside = false;


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

        /**
         * 设置提示信息
         *
         * @param message
         * @return
         */

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * 设置是否显示提示信息
         *
         * @param isShowMessage
         * @return
         */
        public Builder setShowMessage(boolean isShowMessage) {
            this.isShowMessage = isShowMessage;
            return this;
        }

        /**
         * 设置是否可以按返回键取消
         *
         * @param isCancelable
         * @return
         */

        public Builder setCancelable(boolean isCancelable) {
            this.isCancelable = isCancelable;
            return this;
        }

        /**
         * 设置是否可以取消
         *
         * @param isCancelOutside
         * @return
         */
        public Builder setCancelOutside(boolean isCancelOutside) {
            this.isCancelOutside = isCancelOutside;
            return this;
        }

        public LoadingDialog create() {

            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.dialog_loading, null);
            LoadingDialog loadingDailog = new LoadingDialog(context, R.style.CustomDialog);
            TextView msgText = (TextView) view.findViewById(R.id.tipTextView);
            if (isShowMessage) {
                msgText.setText(message);
            } else {
                msgText.setVisibility(View.GONE);
            }
            loadingDailog.setContentView(view);
            loadingDailog.setCancelable(isCancelable);
            loadingDailog.setCanceledOnTouchOutside(isCancelOutside);
            return loadingDailog;

        }
    }
}

引用样式


    

使用

//加载弹窗
LoadingDialog.Builder loadBuilder = new LoadingDialog.Builder(this)
             .setMessage("加载中...")
             .setCancelable(true)//返回键是否可点击
             .setCancelOutside(false);//窗体外是否可点击
 dialog = loadBuilder.create();
 dialog.show();//显示弹窗

dialog.dismiss();//数据加载完成取消弹窗

GitHub

Android自定义Dialog网络加载等待弹窗_第3张图片

你可能感兴趣的:(自定义Dialog,Android)