Я работаю над оптимизацией своего кода в приложении, которое делаю.Я не продвинутый в любом случае, поэтому я мог бы серьезно использовать некоторую помощь.
Я создаю диалоговые окна предупреждений, и я привык использовать это как макет
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();
}
После прочтенияПримеры Google, которые мне удалось создать.
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)