2011-08-30 16 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,因爲它們用於方法中構建的匿名類。這是編譯器的要求。

相關問題