开发者

How to use a custom dialog in all of my application in android

I have a dialog and i have many activities... I need to call the same dailog in all the a开发者_运维知识库ctivities...currently i have written the code in each of the activity but this is not the right way... Can any one help me in this


Simply create a class and within that create a static method(say displayDialog), and within this, copy paste the code that displays your dialog. Now call this static method from anywhere in your project. But you might have to pass the context of the calling activity to the static method.

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


Ok below is my way of implementing dialog its in a static class so I can call it from any activity when I need

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


You should regroup the code that builds the Dialog in a helper class.
Below is an excerpt of a DialogHelper I have built for myself and that I use to show my applications' help files.

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 ...
}

This way I just call

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

from all my applications. That's a lot of parameters but this is workable.

There is a non-nice thing in that code: I use setNegativeButton() for something else than its intended purpose. This is something I should refactor, but this doesn't change anything in the approach.

About the parameters of showHelp(): they are final as they are used in the anonymous classes built within the method. This is a requirement of the compiler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜