整个项目的单个对话框类

时间:2018-11-08 12:00:27

标签: android dialog

对话框应具有2个按钮,每次单击按钮应执行2种不同的操作,并且单击响应应该进入调用类?有什么建议吗?

4 个答案:

答案 0 :(得分:1)

尝试以下代码。

public static void showEmergencyDialog(final Activity activity) {
    final Dialog builder = new Dialog(activity);

    final View dialogView = LayoutInflater.from(activity).inflate(R.layout.emergency_alert_dialog, null);

    final MyTextView emergency_btn = (MyTextView) dialogView.findViewById(R.id.emergency_btn);
    final MyTextView normal_btn = (MyTextView) dialogView.findViewById(R.id.normal_btn);
    final MyTextView sent_btn = (MyTextView) dialogView.findViewById(R.id.sent_btn);
    final MyTextView cancel_btn = (MyTextView) dialogView.findViewById(R.id.cancel_btn);
    final EditText emergency_edt = (EditText) dialogView.findViewById(R.id.emergency_edt);

    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.setContentView(dialogView);
    builder.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(builder.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.gravity = Gravity.CENTER;
    builder.getWindow().setAttributes(lp);

    cancel_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //dismiss dilog code
            builder.dismiss();
        }
    });

    //Emergency push
    emergency_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    builder.show();
}

答案 1 :(得分:0)

这是我几个月前编写的Dialog Handler Class。我没有在任何地方使用它。它确实错过了许多功能,但这对于生成没有按钮的单个AlertDialog很有好处。您通过传递参数来创建DialogHandler类的对象,它可以工作。只需更改方法onPositiveButton()onNegativeButton()的代码。另外,如果您希望每个活动都使用相同的对话框,则可以将constructor中的值硬编码。

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class DialogHandler {
  private Activity activity;
  private String title;
  private String message;
  private Boolean positiveButton;
  private Boolean negativeButton;
  DialogListener dialogListener;

  public void createDialogue(Activity activity, String title, String message,
                           Boolean positiveButton , Boolean negativeButton, DialogListener dialogListener){
    this.activity = activity;
    this.title = title;
    this.message = message;
    this.positiveButton = positiveButton;
    this.negativeButton = negativeButton;
    this.dialogListener = dialogListener;
    generateAlertDialog();
}

private void generateAlertDialog(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle(title)
            .setMessage(message);
            if(positiveButton){
                alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogListener.onPositiveButton();
                    }
                });
            }
            if(negativeButton){
                alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogListener.onNegativeButton();
                    }
                });
            }
            alertDialog.create();
            alertDialog.show();
}

public interface DialogListener{
    void onPositiveButton();

    void onNegativeButton();
}

这里是另一个PermissionHandlerClass。我两个月前为我的一个Android项目写了它,希望它也能帮上忙。

答案 2 :(得分:0)

在项目内的任何地方通过名称DialogFactory创建一个新文件,并在文件内实现以下代码

public class DialogFactory {

public static Dialog createSimpleOkErrorDialog(Context context, String title, String message) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context)
            .setTitle(title)
            .setMessage(message)
            .setNeutralButton("OK", null);
    return alertDialog.create();
}

public static Dialog createSimpleOkErrorDialog(Context context,
                                               @StringRes int titleResource,
                                               @StringRes int messageResource) {

    return createSimpleOkErrorDialog(context,
            context.getString(titleResource),
            context.getString(messageResource));
}

public static Dialog createSimpleOkErrorDialog(Context context, String message) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context)
            .setTitle("ERROR")
            .setMessage(message)
            .setNeutralButton("OK", null);
    return alertDialog.create();
}

public static Dialog createSimpleOkErrorDialog(Context context,
                                               @StringRes int messageResource) {

    return createSimpleOkErrorDialog(context, context.getString(messageResource));
}

public static ProgressDialog createProgressDialog(Context context, String message) {
    ProgressDialog progressDialog = new ProgressDialog(context);
    progressDialog.setTitle("Processing...");
    progressDialog.setMessage(message);
    progressDialog.setCancelable(false);
    return progressDialog;
}

public static ProgressDialog createProgressDialog(Context context,
                                                  @StringRes int messageResource) {
    return createProgressDialog(context, context.getString(messageResource));
}

/**
 * Show dialog.
 *
 * @param ctx       the ctx
 * @param msg       the msg
 * @param btn1      the btn1
 * @param btn2      the btn2
 * @param listener1 the listener1
 * @param listener2 the listener2
 * @return the alert dialog
 */
public static android.app.AlertDialog showDialog(Context ctx, String msg, String btn1,
                                                 String btn2, DialogInterface.OnClickListener listener1,
                                                 DialogInterface.OnClickListener listener2) {

    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
    builder.setMessage(msg).setCancelable(false)
            .setPositiveButton(btn1, listener1);
    if (btn2 != null && listener2 != null)
        builder.setNegativeButton(btn2, listener2);
    int LAYOUT_FLAG;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    }

    android.app.AlertDialog alert = builder.create();
    alert.show();
    return alert;

}

/**
 * Show dialog.
 *
 * @param themeRes  the theme ID
 * @param ctx       the ctx
 * @param msg       the msg
 * @param btn1      the btn1
 * @param btn2      the btn2
 * @param listener1 the listener1
 * @param listener2 the listener2
 * @return the alert dialog
 */
public static android.app.AlertDialog showDialog(int themeRes, Context ctx, String msg, String btn1,
                                                 String btn2, DialogInterface.OnClickListener listener1,
                                                 DialogInterface.OnClickListener listener2) {

    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx, themeRes);
    builder.setMessage(msg).setCancelable(false)
            .setPositiveButton(btn1, listener1);
    if (btn2 != null && listener2 != null)
        builder.setNegativeButton(btn2, listener2);
    int LAYOUT_FLAG;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    }

    android.app.AlertDialog alert = builder.create();
    alert.getWindow().setType(LAYOUT_FLAG);
    alert.show();
    return alert;

}

/**
 * Show dialog.
 *
 * @param ctx       the ctx
 * @param title     the title
 * @param msg       the msg
 * @param btn1      the btn1
 * @param btn2      the btn2
 * @param listener1 the listener1
 * @param listener2 the listener2
 * @return the alert dialog
 */
public static android.app.AlertDialog showDialog(Context ctx, String title, String msg, String btn1,
                                                 String btn2, DialogInterface.OnClickListener listener1,
                                                 DialogInterface.OnClickListener listener2) {

    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
    builder.setTitle(title);
    builder.setMessage(msg).setCancelable(false)
            .setPositiveButton(btn1, listener1);
    if (btn2 != null && listener2 != null)
        builder.setNegativeButton(btn2, listener2);

    android.app.AlertDialog alert = builder.create();
    alert.show();
    return alert;

}

/**
 * Show dialog.
 *
 * @param ctx       the ctx
 * @param msg       the msg
 * @param btn1      the btn1
 * @param btn2      the btn2
 * @param listener1 the listener1
 * @param listener2 the listener2
 * @return the alert dialog
 */
public static android.app.AlertDialog showDialog(Context ctx, int msg, int btn1,
                                                 int btn2, DialogInterface.OnClickListener listener1,
                                                 DialogInterface.OnClickListener listener2) {

    return showDialog(ctx, ctx.getString(msg), ctx.getString(btn1),
            ctx.getString(btn2), listener1, listener2);

}

/**
 * Show dialog.
 *
 * @param ctx      the ctx
 * @param themeRes the theme ID
 * @param msg      the msg
 * @param btn1     the btn1
 * @param btn2     the btn2
 * @param listener the listener
 * @return the alert dialog
 */
public static android.app.AlertDialog showDialog(Context ctx, int themeRes, String msg, String btn1,
                                                 String btn2, DialogInterface.OnClickListener listener) {

    return showDialog(themeRes, ctx, msg, btn1, btn2, listener,
            (dialog, id) -> dialog.dismiss());

}

/**
 * Show dialog.
 *
 * @param ctx      the ctx
 * @param msg      the msg
 * @param btn1     the btn1
 * @param btn2     the btn2
 * @param listener the listener
 * @return the alert dialog
 */
public static android.app.AlertDialog showDialog(Context ctx, String msg, String btn1,
                                                 String btn2, DialogInterface.OnClickListener listener) {

    return showDialog(ctx, msg, btn1, btn2, listener,
            (dialog, id) -> dialog.dismiss());

}

/**
 * Show dialog.
 *
 * @param ctx      the ctx
 * @param msg      the msg
 * @param btn1     the btn1
 * @param btn2     the btn2
 * @param listener the listener
 * @return the alert dialog
 */
public static android.app.AlertDialog showDialog(Context ctx, int msg, int btn1,
                                                 int btn2, DialogInterface.OnClickListener listener) {


    return showDialog(ctx, ctx.getString(msg), ctx.getString(btn1),
            ctx.getString(btn2), listener);

}

/**
 * Show dialog.
 *
 * @param ctx      the ctx
 * @param msg      the msg
 * @param listener the listener
 * @return the alert dialog
 */
public static android.app.AlertDialog showDialog(Context ctx, String msg,
                                                 DialogInterface.OnClickListener listener) {

    return showDialog(ctx, msg, ctx.getString(android.R.string.ok), null,
            listener, null);
}

/**
 * Show dialog.
 *
 * @param ctx      the ctx
 * @param msg      the msg
 * @param listener the listener
 * @return the alert dialog
 */
public static android.app.AlertDialog showDialog(Context ctx, int msg,
                                                 DialogInterface.OnClickListener listener) {

    return showDialog(ctx, ctx.getString(msg),
            ctx.getString(android.R.string.ok), null, listener, null);
}

/**
 * Show dialog.
 *
 * @param ctx the ctx
 * @param msg the msg
 * @return the alert dialog
 */
public static android.app.AlertDialog showDialog(Context ctx, String msg) {

    return showDialog(ctx, msg, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {

            dialog.dismiss();
        }
    });

}

/**
 * Show dialog.
 *
 * @param ctx the ctx
 * @param msg the msg
 * @return the alert dialog
 */
public static android.app.AlertDialog showDialog(Context ctx, int msg) {

    return showDialog(ctx, ctx.getString(msg));

}

/**
 * Show dialog.
 *
 * @param ctx      the ctx
 * @param title    the title
 * @param msg      the msg
 * @param listener the listener
 */
public static void showDialog(Context ctx, int title, int msg,
                              DialogInterface.OnClickListener listener) {

    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
    builder.setMessage(msg).setCancelable(false)
            .setPositiveButton(android.R.string.ok, listener);
    builder.setTitle(title);
    android.app.AlertDialog alert = builder.create();
    alert.show();
}

public static void showEditTextDialog(Context ctx, String title, String hint,
                                      DialogInterface.OnClickListener listener) {
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setTitle(title);

    final EditText input = new EditText(ctx);
    input.setHint(hint);
    input.setSingleLine(true);
    input.setLines(5);
    input.setMaxLines(5);
    input.setGravity(Gravity.LEFT | Gravity.TOP);
    builder.setView(input);
    builder.setPositiveButton(android.R.string.ok, listener);
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

public static void showDialog(Context ctx, String title, String msg,
                              DialogInterface.OnClickListener listener) {

    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
    builder.setMessage(msg).setCancelable(false)
            .setPositiveButton(android.R.string.ok, listener);
    builder.setTitle(title);
    android.app.AlertDialog alert = builder.create();
    alert.show();
}

/**
 * Hide keyboard.
 *
 * @param ctx the ctx
 */
public static final void hideKeyboard(Activity ctx) {

    if (ctx.getCurrentFocus() != null) {
        InputMethodManager imm = (InputMethodManager) ctx
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(ctx.getCurrentFocus().getWindowToken(),
                0);
    }
}

/**
 * Hide keyboard.
 *
 * @param ctx the ctx
 * @param v   the v
 */
public static final void hideKeyboard(Activity ctx, View v) {

    try {
        InputMethodManager imm = (InputMethodManager) ctx
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void showExitDialog(Activity activity) {
    final boolean[] check = new boolean[1];
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setPositiveButton("Yes", (dialog, which) -> {
                activity.finish();
                if (check[0]) {
                    TinyDB.getInstance(activity).putBoolean("dialog_status", true);
                } else {
                    TinyDB.getInstance(activity).putBoolean("dialog_status", false);
                }
            }
    ).setNegativeButton("No", (dialog, which) -> {

    });
    AlertDialog dialog = builder.create();
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    dialog.show();

    Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
    b.setTextColor(ContextCompat.getColor(activity, R.color.colorAccent));
    b = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
    b.setTextColor(ContextCompat.getColor(activity, R.color.colorAccent));

}

}

现在,要想提示对话框的任何操作,只需使用消息调用方法,并将所需的按钮名称命名为

DialogFactory.showDialog(MainActivity.this, "Do you want to exit", "Yes", "Cancel", (dialog, which) -> finish());

请记住,在对话框工厂类中,您必须导入所有相关的类名称。

答案 3 :(得分:0)

使用Handler创建一个Handler.Callback类,如下所示:

class HandlerMsg implements Handler.Callback
    {

        @Override
        public boolean handleMessage(Message msg)
        {
            if(msg.getData().getInt("KEY") == 1)
            {
               //Here, normal_btn clicked
            }
            else
            {
               //Here, emergency_btn clicked
            }

            return true;
        }
    }

以下是 Dialog

的听众
normal_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) 
    {
        Message msg = new Message();
        Bundle b = new Bundle();
        b.putInt("KEY", 1);
        msg.setData(b);

        new Handler(new HandlerMsg()).sendMessage(msg);
        builder.dismiss();
    }
});

emergency_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Message msg = new Message();
        Bundle b = new Bundle();
        b.putInt("KEY", 2);
        msg.setData(b);

        new Handler(new HandlerMsg()).sendMessage(msg);
        builder.dismiss();
    }
});