我正在優化我正在編寫的應用程序上的代碼。我沒有任何進步,所以我可以認真地使用一些幫助。對話框代碼優化
我創建警報對話框,我習慣用這個作爲佈局
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)
我試過這樣做,讓我編輯我的答案,讓你可以看到。 – 2012-01-18 00:27:46