2011-08-30 27 views
0

我有一个对话框,我有很多活动...我需要在所有活动中调用相同的dailog ...目前我已经在每个代码中编写代码活动的,但这是不正确的做法......任何一个可以帮我在这如何在我的所有应用程序中使用自定义对话框android

+1

你相信静态方法吗? – ingsaurabh

+0

是的!!!!!!!!!!!! –

+0

创建自定义对话框相当棘手,但这个链接将有所帮助http://developer.android.com/guide/topics/ui/dialogs.html –

回答

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

你能告诉你如何在你的活动中调用它吗? – Yuri

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(); 
} 
} 
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,因为它们用于方法中构建的匿名类。这是编译器的要求。

相关问题