我正在努力优化我正在制作的应用程序上的代码。我没有任何进步,所以我可以认真地使用一些帮助。
我正在创建警报对话框,我习惯将其用作布局
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();
}
我的主要问题是我正在制作一个Yes / No对话框,我想在点击Yes按钮时做一些不同的事情。我想以类似于参数的方式传递这个,所以我不重复我以前使用的代码。有没有办法做到这一点?非常感谢。
编辑:
我尝试过以下不起作用的代码。
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 :(得分:0)
您必须在onCreateDialog函数中为Yes按钮定义DialogInterface.OnClickListener对象,然后将其作为参数传递给createDialog函数,您可以使用它来构建新对话框。
答案 1 :(得分: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);
}
问题仍然适用;如果有人对此有更好的解决方案,请分享。