简体   繁体   English

如何减少android中main-activity的警报对话代码?

[英]How do I reduce alert-dialogue code from main-activity in android?

I have a class where I am writing three different alert dialogue. 我有一堂课,我在写三种不同的警报对话框。 These three alert dialogue extends the line of code of this class drastically. 这三个警报对话框极大地扩展了此类代码的范围。 So for re-factoring purpose, I want to make base class for three alert dialogue and use this class in the main activity. 因此,出于重构目的,我想为三个警报对话框创建基类,并在主要活动中使用该类。 Can anyone suggest me how to achieve this? 谁能建议我如何实现这一目标? My three alert dialogue are given below: 我的三个警报对话框如下:

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();

Complete solution Try this is a Generic Alert dialog with cutom tilte,message,yes,no button caption 完整的解决方案尝试这是一个通用警报对话框,其中包含皮肤倾斜,消息,是,没有按钮标题

1) Createa Interface 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) Generalize method for confirm dialog. 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) How to use 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) {

                    }
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM