美文网首页Android
AlertDialog封装(2018.10.15)

AlertDialog封装(2018.10.15)

作者: 瑟闻风倾 | 来源:发表于2018-10-15 13:12 被阅读10次

1. AlertDialogUtil类

package com.jack.feiYan2.tool;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import com.jack.workticket.R;

/**
 * Created by liy on 2018-04-09.
 */
public class AlertDialogUtil {
    public static AlertDialog alertDialog = null;
    /**
     * 显示提示消息的对话框:仅提示
     * @param context
     * @param title
     * @param message
     */
    public static void warningDialog(Context context,String title, String message) {
        if(alertDialog == null){
            alertDialog = new AlertDialog.Builder(context, R.style.Theme_AppCompat_Light_Dialog_Alert)
                    .setTitle(title)
                    .setMessage(message)
                    .setPositiveButton(R.string.button_sure, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).show();
        }else{
            if(alertDialog.isShowing()){

            }else{
                alertDialog.show();
            }
        }
    }

    /**
     * 弹出对话框并有2个按钮可选择处理
     * @param context
     * @param title
     * @param message
     */
    public static void TwoChoiceDialog(Context context,String title, String message,String[] items,final TwoChoiceHandle choiceHandle) {
        if(items.length!=2){
            return;
        }
        if(alertDialog == null){
            alertDialog = new AlertDialog.Builder(context, R.style.Theme_AppCompat_Light_Dialog_Alert)
                    .setTitle(title)
                    .setMessage(message)
                    .setPositiveButton(items[0], new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            choiceHandle.onPositiveButtonHandle();
                        }
                    })
                    .setNegativeButton(items[1], new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            choiceHandle.onNegativeButtonHandle();
                        }
                    }).show();
        }else{
            if(alertDialog.isShowing()){

            }else{
                alertDialog.show();
            }
        }
    }


    public interface TwoChoiceHandle {
        void onPositiveButtonHandle();
        void onNegativeButtonHandle();
    }

    /**
     * 弹出对话框并有3个按钮可选择处理
     * @param activity
     * @param title
     * @param message
     */
    public static void ThreeChoiceDialog(Activity activity, String title, String message, String[] items, final ThreeChoiceHandle choiceHandle) {
        if(items.length!=3){
            return;
        }
        /*if(alertDialog == null){*/
            alertDialog = new AlertDialog.Builder(activity, R.style.Theme_AppCompat_Light_Dialog_Alert)
                    .setTitle(title)
                    .setMessage(message)
                    .setIcon(R.drawable.ic_comfirm)
                    .setPositiveButton(items[0], new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            choiceHandle.onPositiveButtonHandle();
                        }
                    })
                    .setNegativeButton(items[1], new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            choiceHandle.onNegativeButtonHandle();
                        }
                    })
                    .setNeutralButton(items[2], new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            choiceHandle.onNeutralButtonButtonHandle();
                        }
                    }).show();
        /*}else{*/
            if(alertDialog.isShowing()){

            }else{
                if (!activity.isFinishing()){
                    alertDialog.show();
                }
            }
       /* }*/
    }

    public interface ThreeChoiceHandle {
        void onPositiveButtonHandle();
        void onNegativeButtonHandle();
        void onNeutralButtonButtonHandle();
    }

}

2. 调用

  • 显示提示消息的对话框(仅提示)
    AlertDialogUtil.warningDialog(activity,activity.getString(R.string.SocketTimeoutException),activity.getString(R.string.SocketTimeoutException_des));
  • 弹出对话框并有3个按钮可选择处理
final String[] reasons = new String[]{SUPPLEMENT_REASON_CUT_PARTS,SUPPLEMENT_REASON_THREAD,SUPPLEMENT_REASON_ACCESSORIES};
AlertDialogUtil.ThreeChoiceDialog(activity, "please select the reason of noLoad", "",reasons, new AlertDialogUtil.ThreeChoiceHandle() {
                            @Override
                            public void onPositiveButtonHandle() {
                                ToastUtil.show(activity,reasons[0]);
                            }

                            @Override
                            public void onNegativeButtonHandle() {
                                ToastUtil.show(activity,reasons[1]);
                            }

                            @Override
                            public void onNeutralButtonButtonHandle() {
                                ToastUtil.show(activity,reasons[2]);
                            }
                        });

3. 其他

  • R.drawable.ic_comfirm资源文件
<vector android:height="24dp" android:viewportHeight="1024.0"
    android:viewportWidth="1024.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#f4ea2a" android:pathData="M505.2,0C222.4,3.8 -3.7,236.1 0,518.9c3.8,282.6 236.1,508.8 518.8,505.1 282.7,-3.8 508.8,-236.1 505.1,-518.9C1020.2,222.5 787.9,-3.7 505.2,0zM503.7,823.7l-2.8,-0.1c-43.5,-1.3 -74.2,-33.4 -73,-76.3 1.2,-42.1 32.6,-72.8 74.8,-72.8l2.5,0c44.7,1.3 75.1,33.1 73.8,77.2C577.7,794.1 546.8,823.7 503.7,823.7zM686.8,460.1c-10.2,14.6 -32.8,32.6 -61.1,54.7l-31.3,21.6c-17.2,13.3 -27.5,25.9 -31.4,38.2 -3.1,9.7 -4.6,12.3 -4.8,32.1L558.2,611.7l-119.3,0 0.4,-10.1c1.4,-41.5 2.5,-65.9 19.7,-86.1 27,-31.7 86.5,-70 89,-71.6 8.5,-6.4 15.7,-13.7 21.1,-21.6 12.5,-17.3 18.1,-30.9 18.1,-44.2 0,-18.5 -5.5,-35.7 -16.4,-50.9 -10.5,-14.7 -30.3,-22.2 -59,-22.2 -28.5,0 -48,9 -59.6,27.6 -12,19.1 -18.1,39.1 -18.1,59.5l0,5.1 -123,0 0.2,-5.3c3.2,-75.3 30.1,-129.6 79.9,-161.2 31.3,-20.1 70.3,-30.4 115.7,-30.4 59.5,0 109.8,14.5 149.3,43 40.1,28.9 60.4,72.2 60.4,128.6C716.5,403.5 706.5,433.2 686.8,460.1z"/>
</vector>

  • 常量管理GlobalConstant类
package com.jack.feiYan2.tool;

import android.os.Environment;

import java.io.File;

/**
 * Created by liy on 2018-03-29.
 */

public class GlobalConstant {
    public static GlobalConstant globalConstant;

    public GlobalConstant(){

    }
    public static GlobalConstant getGlobalConstant(){
        if(globalConstant==null){
            globalConstant = new GlobalConstant();
        }
        return globalConstant;
    }

    public static final String SUPPLEMENT_REASON_CUT_PARTS = "Cut Parts";
    public static final String SUPPLEMENT_REASON_THREAD = "Thread";
    public static final String SUPPLEMENT_REASON_ACCESSORIES = "Accessories";

}

相关文章

网友评论

    本文标题:AlertDialog封装(2018.10.15)

    本文链接:https://www.haomeiwen.com/subject/lklfzftx.html