2012-11-07 33 views
1

感謝您成爲一個了不起的資源,我有一個關於實施了Android SDK apidemo AlertDialogSamples.java問題。基本上,我想要的AlertDialogSamples類添加到我的應用程序,並在其上呼籲從我的任何活動的各個alertdialogs(即DIALOG_YES_NO_MESSAGEDIALOG_PROGRESS,...)。我很驚訝所有的在線示例在每個活動類中都有alertdialog代碼。機器人實現AlertDialogSamples類

我想這個想法幾次迭代:

public class My1Activity extends SherlockActivity implements View.OnClickListener { 
    ... 
    AlertDialogSamples alert = new AlertDialogSamples(); 
    ... 
    private void changeEmailCommand(){ 
     alert.showAlertDialog(My1Activity.this, DIALOG_TEXT_ENTRY, title); 

然後:

public class AlertDialogSamples extends Activity { 
    ... 
public void showAlertDialog(Context context, int i, String title) { 
     showAlertDialog(context, i, title); 
    ... (rest is existing code and modifying the .setTitle to accept title message) 
    } 

,也沒有運氣得到的alertdialog出現(只是崩潰)

是否有如何用意向去做? ASYNCH?

回答

0

我用下面的代碼顯示進度對話框:

  1. 創建靜態方法的實用程序類。
  2. 從您的活動叫他們時要顯示一個對話,例如:Utility.showActionIndicator(「您的信息」,Acitivty.this);和Utility.dismissActionIndicator();

你可以把這個例子中的幫助,試試你的運氣。

代碼更新,增加了實用的方法來顯示警報對話框 - 只需撥打Utility.showAlertDialog(YourActivity.this)

更新:(提示 - 添加Morfy一隻受傷的獅子的方法,這一類 - 感謝Morfy一個受傷獅子的代碼。它真棒)

public class Utility { 

    private static ProgressDialog actionIndicator = null; 
    private static AlertDialog.Builder alertDialogBuilder = null; 

    private Utility() { 

    } 

    public static void showAlertDialog(Activity activity) { 

     if (alertDialogBuilder == null) { 
      alertDialogBuilder = new AlertDialog.Builder(activity); 
     } 
      // set title 
      alertDialogBuilder.setTitle("Your Title"); 

      // set dialog message 
      alertDialogBuilder 
       .setMessage("Click yes to exit!") 
       .setCancelable(false) 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 

        } 
        }) 
       .setNegativeButton("No",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 

         dialog.cancel(); 
        } 
       }); 

       // create alert dialog 
       AlertDialog alertDialog = alertDialogBuilder.create(); 

       // show it 
       alertDialog.show(); 

    } 

    public static void showActionIndicator(String message,Activity activity) { 

     if (actionIndicator == null) { 
      actionIndicator = 
      new ProgressDialog(activity); 
     } 
     actionIndicator.setCancelable(false); 
     actionIndicator.setCanceledOnTouchOutside(false); 
     actionIndicator.setMessage(message); 
     if (!actionIndicator.isShowing()) { 
      try { 
       actionIndicator.show(); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       Log.d("exception","something went wrong",e); 
      } 
     } 
     System.out.println("Dialog is showing"); 
    } 

    public static void changeActionIndicatorMessage(String message) { 

     if (actionIndicator != null && actionIndicator.isShowing()) { 
      actionIndicator.setMessage(message); 
     } 
    } 

    public static void dismissActionIndicator() { 
     if (actionIndicator != null && actionIndicator.isShowing()) { 
      try { 
       actionIndicator.dismiss(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      actionIndicator = null; 
      System.out.println("Dialog dismissed"); 
     } 
    } 

} 
0
public static void showAlerDialog(Context context, String title, 
     String msg, AlertDialogDelegate delegate, int code) { 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
    alertDialog.setTitle(title); 
    alertDialog.setMessage(msg); 
    alertDialog.setCancelable(false); 

    if (title.equalsIgnoreCase(ERROR)) { 
     alertDialog.setIcon(context.getResources().getDrawable(
       R.drawable.error_icon)); 
    } else if (title.equalsIgnoreCase(SUCCESS)) { 
     alertDialog.setIcon(context.getResources().getDrawable(
       R.drawable.success)); 
    } else { 
     alertDialog.setIcon(context.getResources().getDrawable(
       R.drawable.warning)); 
    } 

    alertDialog.setPositiveButton("OK", new AlertDialogListenter(delegate, 
      code)); 
    alertDialog.show(); 

} 

然後調用AlertDialog像

ClassNam e.showAlerDialog(上下文,標題,味精,委託,代碼);

此背景下 - >上下文

標題 - > alertdialog標題 - 字符串

味精---> alertdialog味精 - 字符串

代表--->接口的對象,調用一個函數一些是 - 任何情況下

代碼 - 整型 - 對於一些不同的行動代表

0
public static void showAlerDialog(Context context, String title, 
     String msg, AlertDialogDelegate delegate, int code) { 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
    alertDialog.setTitle(title); 
    alertDialog.setMessage(msg); 
    alertDialog.setCancelable(false); 

    if (title.equalsIgnoreCase(ERROR)) { 
     alertDialog.setIcon(context.getResources().getDrawable(
       R.drawable.error_icon)); 
    } else if (title.equalsIgnoreCase(SUCCESS)) { 
     alertDialog.setIcon(context.getResources().getDrawable(
       R.drawable.success)); 
    } else { 
     alertDialog.setIcon(context.getResources().getDrawable(
       R.drawable.warning)); 
    } 

    alertDialog.setPositiveButton("OK", new AlertDialogListenter(delegate, 
      code)); 
    alertDialog.show(); 

} 
+0

請在回答中提供描述。 –