多次使用相同的 AlertDialog 框并更改一个变量
Using same AlertDialog box multiple times with one variable change
在我的应用程序中,我想在几个地方使用 AlertDialog
框。我可以通过在任何我想要 AlertDialog
框的地方使用代码来做到这一点。但这似乎是一种浪费,因为它是相同的代码,只有一个变化。如何通过更改变量为 AlertDialog
框使用相同的代码?
这是我的 AlertDialog
Box
代码
private void showInternetConenctionAlert(String alert, String title) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setMessage(alert);
alertDialog.setTitle(title);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(topten.getFile()));
activity.startActivity(intent);
}
});
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.show();
}
这里 onClick
为阳性 Button
我用过
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(topten.getFile()));
activity.startActivity(intent);
这里唯一需要改的地方就是这个了
topten.getFile()
我应该采取什么方法来做到这一点?
你几乎已经自己完成了,只需在 showInternetConenctionAlert
函数中添加另一个参数,例如String uriString
,并将 topten.getFile()
替换为 uriString
。
private void showInternetConenctionAlert(String alert, String title, final String uriString) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setMessage(alert);
alertDialog.setTitle(title);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriString));
activity.startActivity(intent);
}
});
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.show();
}
- 通过扩展 DialogFragment 并在 onCreateDialog() 回调方法中创建一个 AlertDialog 来创建一个单独的 class。
- 实例化一个 AlertDialog.Builder 及其构造函数
AlertDialog.Builder 建造者 = 新 AlertDialog.Builder(getActivity());
任何你需要警告对话框的地方。
看看这个link,它可能会准确地解决你的问题
http://developer.android.com/guide/topics/ui/dialogs.html
在我的应用程序中,我想在几个地方使用 AlertDialog
框。我可以通过在任何我想要 AlertDialog
框的地方使用代码来做到这一点。但这似乎是一种浪费,因为它是相同的代码,只有一个变化。如何通过更改变量为 AlertDialog
框使用相同的代码?
这是我的 AlertDialog
Box
private void showInternetConenctionAlert(String alert, String title) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setMessage(alert);
alertDialog.setTitle(title);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(topten.getFile()));
activity.startActivity(intent);
}
});
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.show();
}
这里 onClick
为阳性 Button
我用过
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(topten.getFile()));
activity.startActivity(intent);
这里唯一需要改的地方就是这个了
topten.getFile()
我应该采取什么方法来做到这一点?
你几乎已经自己完成了,只需在 showInternetConenctionAlert
函数中添加另一个参数,例如String uriString
,并将 topten.getFile()
替换为 uriString
。
private void showInternetConenctionAlert(String alert, String title, final String uriString) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setMessage(alert);
alertDialog.setTitle(title);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriString));
activity.startActivity(intent);
}
});
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.show();
}
- 通过扩展 DialogFragment 并在 onCreateDialog() 回调方法中创建一个 AlertDialog 来创建一个单独的 class。
- 实例化一个 AlertDialog.Builder 及其构造函数 AlertDialog.Builder 建造者 = 新 AlertDialog.Builder(getActivity()); 任何你需要警告对话框的地方。
看看这个link,它可能会准确地解决你的问题 http://developer.android.com/guide/topics/ui/dialogs.html