簡體   English   中英

如何減少android中main-activity的警報對話代碼?

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

我有一堂課,我在寫三種不同的警報對話框。 這三個警報對話框極大地擴展了此類代碼的范圍。 因此,出於重構目的,我想為三個警報對話框創建基類,並在主要活動中使用該類。 誰能建議我如何實現這一目標? 我的三個警報對話框如下:

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

完整的解決方案嘗試這是一個通用警報對話框,其中包含皮膚傾斜,消息,是,沒有按鈕標題

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) {

                    }
                });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM