1

我有一堂课,我正在写三个不同的警报对话。这三个警告对话框极大地扩展了该类的代码行。所以为了重构的目的,我想为三个警报对话创建基类,并在主要活动中使用这个类。谁能建议我如何实现这一目标?我的三个警报对话如下:

public boolean onJsConfirm(WebView view, String url, String message,
                final android.webkit.JsResult result) {
            new AlertDialog.Builder(currentActivity)
                    .setTitle("Confirmation")
                    .setMessage(message)
                    .setPositiveButton(android.R.string.yes,
                            new AlertDialog.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    result.confirm();
                                }
                            })
                    .setNegativeButton(android.R.string.no,
                            new AlertDialog.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    result.cancel();
                                }
                            }).setCancelable(false).create().show();

            return true;
        }



        public boolean onJsAlert(WebView view, String url, String message,
                final android.webkit.JsResult result) {
            new AlertDialog.Builder(currentActivity)
                    .setTitle("Alert !")
                    .setMessage(message)
                    .setPositiveButton(android.R.string.ok,
                            new AlertDialog.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    result.confirm();
                                }
                            }).setCancelable(false).create().show();

            return true;
        } 


    public void onGeolocationPermissionsShowPrompt(final String origin,
                final GeolocationPermissions.Callback callback) {

            final boolean remember = true;

            AlertDialog.Builder builder = new AlertDialog.Builder(
                    WebviewActivity.this);
            builder.setTitle("Locations");
            builder.setMessage(" Would like to use your Current Location")
                    .setCancelable(true)
                    .setPositiveButton("Allow",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    callback.invoke(origin, true, remember);

                                    SharedPreferences pref = currentActivity
                                            .getPreferences(currentActivity.MODE_PRIVATE);
                                    SharedPreferences.Editor editor = pref
                                            .edit();
                                    editor.putBoolean("isLocationAvailable",
                                            true);
                                    editor.commit();

                                    webview.loadUrl(getUrl(gps.getLatitude(),
                                            gps.getLongitude(), "true"));
                                }
                            })
                    .setNegativeButton("Don't Allow",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    callback.invoke(origin, false, remember);
                                    webview.loadUrl(getUrl("", "", "false"));

                                }
                            });
            AlertDialog alert = builder.create();
            alert.show();
4

1 回答 1

7

完整的解决方案试试这是一个带有自定义倾斜、消息、是、没有按钮标题的通用警报对话框

1)创建接口

import android.content.DialogInterface;

public interface AlertMagnatic {

    public abstract void PositiveMethod(DialogInterface dialog, int id);
    public abstract void NegativeMethod(DialogInterface dialog, int id);
}

2) 概括确认对话框的方法。

public static void getConfirmDialog(Context mContext,String title, String msg, String positiveBtnCaption, String negativeBtnCaption, boolean isCancelable, final AlertMagnatic target) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

        int imageResource = android.R.drawable.ic_dialog_alert;
        Drawable image = mContext.getResources().getDrawable(imageResource);

        builder.setTitle(title).setMessage(msg).setIcon(image).setCancelable(false).setPositiveButton(positiveBtnCaption, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                target.PositiveMethod(dialog, id);
            }
        }).setNegativeButton(negativeBtnCaption, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                target.NegativeMethod(dialog, id);
            }
        });

        AlertDialog alert = builder.create();
        alert.setCancelable(isCancelable);
        alert.show();
        if (isCancelable) {
            alert.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface arg0) {
                    target.NegativeMethod(null, 0);
                }
            });
        }
    }

3) 如何使用

getConfirmDialog(getString(R.string.logout), getString(R.string.logout_message), getString(R.string.yes), getString(R.string.no), false,
                new AlertMagnatic() {

                    @Override
                    public void PositiveMethod(final DialogInterface dialog, final int id) {}

                    @Override
                    public void NegativeMethod(DialogInterface dialog, int id) {

                    }
                });
于 2013-10-03T10:39:25.280 回答