如何在android中的所有应用程序中使用自定义对话框

时间:2011-08-30 09:29:28

标签: android dialog

我有一个对话框,我有很多活动...我需要在所有活动中调用相同的dailog ...目前我已经在每个活动中编写了代码,但这不是正确的方法......任何人都可以帮助我

3 个答案:

答案 0 :(得分:1)

只需创建一个类,然后在其中创建一个静态方法(比如displayDialog),然后在其中复制粘贴显示对话框的代码。现在从项目的任何位置调用此静态方法。但是您可能必须将调用活动的上下文传递给静态方法。

public class dialogClass {

static Dialog dialog=null;
public static void exitApp_Dialog(Context context,String title,String message){

     AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
      alertbox.setTitle("Warning");
     alertbox.setMessage("Exit Application?");
     alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface arg0, int arg1) {
             activity.finish();
         }
     });
     alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface arg0, int arg1) {

         } 
     });
     alertbox.show();
}
}

答案 1 :(得分:0)

以下是我在静态类中实现对话框的方式,所以我可以在需要时从任何活动中调用它

public static void showProgressDialog(Context mContext, String text, boolean cancellable)
{
    removeDialog();
    mDialog = new Dialog(mContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    LayoutInflater mInflater = LayoutInflater.from(mContext);
    View layout = mInflater.inflate(R.layout.popup_example, null);
    mDialog.setContentView(layout);

    TextView mTextView = (TextView) layout.findViewById(R.id.text);
    if (text.equals(""))
        mTextView.setVisibility(View.GONE);
    else
        mTextView.setText(text);

    mDialog.setOnKeyListener(new DialogInterface.OnKeyListener()
    {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
        {
            switch (keyCode)
            {
            case KeyEvent.KEYCODE_BACK:
                return true;
            case KeyEvent.KEYCODE_SEARCH:
                return true;
            }
            return false;

        }
    });

    mDialog.setCancelable(cancellable);

    mDialog.show();
}

public static void removeDialog()
{
    if (mDialog != null)
        mDialog.dismiss();
}

答案 2 :(得分:0)

您应该重新组合在辅助类中构建Dialog的代码 下面是我为自己构建的DialogHelper的摘录,用于显示我的应用程序的帮助文件。

public class DialogHelper {

public static AlertDialog showHelp(final Context ctx, final int resTitle, final int resFilename, final int resOk, final int resViewOnline, final int resOnlineUrl) {
        final WebView webview = new WebView(ctx);
        webview.loadUrl(ctx.getString(resFilename));

        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setTitle(resTitle)
                .setView(webview)
                .setCancelable(false)
                .setPositiveButton(resOk, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                })
                .setNegativeButton(resViewOnline, new DialogInterface.OnClickListener()         {
                    public void onClick(DialogInterface dialog, int id) {
                        final Uri uri = Uri.parse(ctx.getString(resOnlineUrl));
                        final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        ctx.startActivity(intent);
                    }
                }
            );

            final AlertDialog dlg = builder.create();
            dlg.show();

            return dlg;
        }
    }

    ... other kinds of dialog take place here ...
}

这样我只需要打电话

DialogHelper.showHelp(context, R.string.helpTitle, R.string.localizedFilename, R.string.labelOk, R.string.labelViewOnlineHelp, R.string.onlineHelpUrl); 

来自我的所有应用程序。这是很多参数,但这是可行的。

该代码中有一个不太好的东西:我将setNegativeButton()用于其他目的而非预期目的。这是我应该重构的东西,但这并没有改变方法中的任何内容。

关于showHelp()的参数:它们是final,因为它们在方法中构建的匿名类中使用。这是编译器的要求。