私は自分のアプリケーションで自分のコードを最適化しようとしています。私はどのように進んでいないので、真剣にいくつかの助けを使うことができます。ダイアログボックスコードの最適化
私は警告ダイアログボックスを作成しています、と私はこれを作成するために管理しているGoogleの例を読んだ後、レイアウト
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)
私はそれをやってみましたが、あなたが見ることができるように私の答えを編集させてください。 –