2012-01-18 52 views
1

我正在優化我正在編寫的應用程序上的代碼。我沒有任何進步,所以我可以認真地使用一些幫助。對話框代碼優化

我創建警報對話框,我習慣用這個作爲佈局

private void doScript2() { 
    AlertDialog.Builder alert_box=new AlertDialog.Builder(this); 
    alert_box.setIcon(R.drawable.caution); 
    alert_box.setTitle("Caution"); 
    alert_box.setMessage("Mount external sd as NTFS at boot?"); 
    alert_box.setPositiveButton("Yes",new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      Toast.makeText(getApplicationContext(), "External SD will be mounted",Toast.LENGTH_SHORT).show(); 
      try { 
       .... 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } catch (RootToolsException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
    alert_box.setNegativeButton("No", new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
     } 
    }); 
    alert_box.show(); 
} 

閱讀谷歌的例子我已成功創建之後。

private static final int DIALOG_TWRESTORE = 202; 
private static final int DIALOG_TWRESTART = 204; 
private static final int DIALOG_TWONESIX = 205; 
private static final int DIALOG_TWONENINE = 206; 
private static final int DIALOG_TWTWOFOUR = 207; 

protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DIALOG_TWRESTORE: 
     return createDialog("Restore", "Are you sure you want to restore your TW?"); 
    case DIALOG_TWRESTART: 
     return createDialog("Restart", "Restart TW?"); 
    case DIALOG_TWONESIX: 
     return createDialog("Resize to 6x6", "Are you blablabla of TW?"); 
    case DIALOG_TWONENINE: 
     return createDialog("Resize to 5x5", "Are you on blablabla of TW?"); 
    case DIALOG_TWTWOFOUR: 
     return createDialog("Resize to 4x5", "Are you on blablabla of TW?"); 
    default: 
     return super.onCreateDialog(id); 
    } 
} 

private Dialog createDialog(String title, String message) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle(title) 
    .setIcon(R.drawable.question) 
    .setMessage(message) 
    .setCancelable(false) 
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      //I want to pass on a parameter from the above class to use within the dialog 
      **parametergoeshere*();    
     } 
    }) 
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     } }); 
    return builder.create(); 
} 

我的主要問題是,我做我想要做的事時是按鈕的點擊不同的是/否對話框。我想以類似的方式傳遞這個參數,所以我不重複我以前使用的代碼。有沒有辦法做到這一點?提前致謝。

編輯:

我曾嘗試下面的代碼不工作。

protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DIALOG_TWRESTORE: 
     return createDialog("Restore", "Are you sure you want to restore your TW?", Commands.doTWBackup()); 
    default: 
     return super.onCreateDialog(id); 
    } 
} 

private Dialog createDialog(String title, String message, Object param) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle(title) 
    .setIcon(R.drawable.question) 
    .setMessage(message) 
    .setCancelable(false) 
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      //I want to pass on a parameter from the above class to use within the dialog 
      param;    
     } 
    }) 
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     } }); 
    return builder.create(); 
} 

錯誤我收到如下:

Syntax error, insert "AssignmentOperator Expression" to complete Expression 

The method createDialog(String, String, Object) in the type Launcher is not applicable for the arguments (String, String, void) 

回答

0

你必須定義在onCreateDialog功能是按鈕的DialogInterface.OnClickListener對象,然後將它傳遞給createDialog函數作爲參數,在那裏你會用它來建立新的對話框。

+0

我試過這樣做,讓我編輯我的答案,讓你可以看到。 – 2012-01-18 00:27:46

0

我落得這樣做最好的方法是:

protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DIALOG_RIGHTTOLEFT: 
     return new AlertDialog.Builder(this).setTitle("Caution").setIcon(android.R.drawable.ic_dialog_alert) 
       .setMessage("Do you need to apply Right to Left Patch? This is NOT Revertable!") 
       .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         RootCommands.doRightToLeft(); 
         Toast.makeText(getApplicationContext(), "RTL Patch Applied. Please Reboot.",Toast.LENGTH_SHORT).show(); 
        }}).setNegativeButton(android.R.string.no, null).create(); 
    case DIALOG_MOUNTNTFS: 
     return new AlertDialog.Builder(this).setTitle("Caution").setIcon(android.R.drawable.ic_dialog_alert) 
       .setMessage("This will mount your external SDCard as NTFS and Break apps2sd. Continue?") 
       .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         RootCommands.doMountNTFS(); 
         Toast.makeText(getApplicationContext(), "External SD Mounted as NTFS",Toast.LENGTH_SHORT).show(); 
        }}).setNegativeButton(android.R.string.no, null).create(); 
    case DIALOG_MOUNTONBOOT: 
     return new AlertDialog.Builder(this).setTitle("Caution").setIcon(android.R.drawable.ic_dialog_alert) 
       .setMessage("Mount external sd as NTFS at boot? This might cause issues.") 
       .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         RootCommands.doMountOnBoot(); 
         Toast.makeText(getApplicationContext(), "Reboot to take effect.",Toast.LENGTH_SHORT).show(); 
        }}).setNegativeButton(android.R.string.no, null).create(); 
    default: 
     return super.onCreateDialog(id); 
    } 

問題仍然適用;如果有人提供更好的解決方案,請分享。