android簡単自封DialogUtil

6867 ワード

public class DialogUtil {
	//           
    private static WeakReference ref_alert;
    private static WeakReference ref_progress;

    public static void alertDialog(Context context, String title, String message) {
        alertDialog(context,null,null,title,message,true,null);
    }
    public static void alertDialog(Context context, String title, String message,boolean isCancel, final Runnable taskPositive) {
        alertDialog(context,"ok","cancel",title,message,isCancel,taskPositive);
    }
    public static void dismiss(){
        if (ref_progress.get() !=null&& ref_progress.get().isShowing()){
            ref_progress.get().dismiss();
        }
        if (ref_alert.get()!=null&&ref_alert.get().isShowing()){
            ref_alert.get().dismiss();
        }
    }
    public static void alertDialog(Context context, String positiveText,String negativeText,String title, String message,boolean isCancel, final Runnable taskPositive) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        if (!TextUtils.isEmpty(title))
            builder.setTitle(title);
        if (!TextUtils.isEmpty(title))
            builder.setMessage(message);
        builder.setCancelable(isCancel);
        if (!TextUtils.isEmpty(positiveText)) {
            builder.setPositiveButton(positiveText, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (taskPositive != null) {
                        taskPositive.run();
                    }
                    dialog.dismiss();
                }
            });
        }
        if (!TextUtils.isEmpty(negativeText)) {
            builder.setNegativeButton(negativeText, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int i) {
                    dialog.dismiss();
                }
            });
        }
        AlertDialog  alertDialog = builder.create();
        ref_alert = new WeakReference(alertDialog);
        alertDialog.show();
    }

//    public static void loading(Context context,boolean isCancel) {
//
//        ProgressBar progressBar = new ProgressBar(context);
//        dialog = new Dialog(context);
//        dialog.requestWindowFeature(1);
//        dialog.getWindow().setBackgroundDrawableResource(R.drawable.transparent_bg);
//        Window window = dialog.getWindow();
//        window.setAttributes( window.getAttributes());
//        dialog.setContentView(progressBar);
//        dialog.show();
//    }

    public static void customView(Context context, View view, Runnable runTask) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(view);
        if (runTask!=null){
            runTask.run();
        }
        AlertDialog alertDialog = builder.create();
        ref_alert = new WeakReference(alertDialog);
        alertDialog.show();
    }

    public static void customView(Context context, @LayoutRes int view, Runnable runTask) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(view);
        if (runTask!=null){
            runTask.run();
        }
        AlertDialog alertDialog = builder.create();
        ref_alert = new WeakReference(alertDialog);
        alertDialog.show();
    }

    /**
     *       
     *
     * @param context
     * @param title                 null
     * @param message          
     * @param cancelable                 true      
     * @param cancelListener dialog     
     */
    public static void showLoadingDialog(Context context, String title, String message, Boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
        ProgressDialog  progressDialog = ProgressDialog.show(context, title, message, false,
                cancelable, cancelListener);
        ref_progress = new WeakReference<>(progressDialog);
    }

    public static void showLoadingDialog(Context context, String title, String message, Boolean cancelable) {
        ProgressDialog progressDialog = ProgressDialog.show(context, title, message, false,
                cancelable);
        ref_progress = new WeakReference<>(progressDialog);
    }

    /**
     *      
     *
     * @param context
     * @param title
     * @param message
     * @param cancelable
     */
    public static void showHorizhontalLoadingDialog(Context context, String title, String message, Boolean cancelable) {
        ProgressDialog  progressDialog = new ProgressDialog(context);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//        
        progressDialog.setCancelable(true);//           Back   
        progressDialog.setCanceledOnTouchOutside(false);//      Dialog     Dialog   
        progressDialog.setIcon(null);//      title   ,      
        progressDialog.setTitle(title);
        progressDialog.setMessage(message);
        progressDialog.setMax(100);
        progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "  ",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "  ",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        progressDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "  ",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        progressDialog.show();
        ref_progress = new WeakReference<>(progressDialog);
//        AppOperator.runOnThread(new Runnable() {
//
//            @Override
//            public void run() {
//                int i = 0;
//                while (i < 100) {
//                    try {
//                        Thread.sleep(200);
//                        //         ,              
//                        progressDialog.incrementProgressBy(1);
//                        // progressDialog.incrementSecondaryProgressBy(10)//         
//                        i++;
//                    } catch (Exception e) {
//                    }
//                }
//                //          Dialog
//                progressDialog.dismiss();
//            }
//        });
    }
}