Android:将参数传递给警报对话框
Android: Passing parameters to Alert Dialog
我正在展示一个带有 OK/Cancel 按钮的简单警报对话框。
当用户单击确定时,一些代码运行 - 需要一个参数。
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder
.setTitle("Are you sure?")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//TODO: Do something with parameter.
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
如何将参数传递给AlertDialog?
如果您将变量声明为 final
,那么您可以在调用 AlertDialog.Builder() 之前在代码中设置它,然后在 onClick() 中访问它,如下所示:
final int someParameter = someValue;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
alertDialogBuilder
.setTitle("Are you sure?")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do something with parameter.
doSomeStuff(someParameter);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
这样,someParameter 通过函数 closure 隐式传递给 onClick(),因此无需继承 AlertDialog 或向您的 Activity 添加额外的成员变量。
你也可以在应用中使用静态变量。定义
public 静态字符串 SumValue="";
假设我的调用 class 是 MainActivity Class 那么,我可以在调用对话框之前初始化变量,例如
MainActivity.SumValue="测试值";
addcartdialog addcartdialog = new addcartdialog();
addcartdialog.show( ((AppCompatActivity) 上下文).getSupportFragmentManager(),"添加购物车");
在添加购物车对话框中class
你可以分配给
textView = view.findViewById(R.id.servingsTxt);
textView = MainActivity.SumValue;
通过这种方式,您可以在 alertdialog 中使用多个变量。
我正在展示一个带有 OK/Cancel 按钮的简单警报对话框。 当用户单击确定时,一些代码运行 - 需要一个参数。
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder
.setTitle("Are you sure?")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//TODO: Do something with parameter.
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
如何将参数传递给AlertDialog?
如果您将变量声明为 final
,那么您可以在调用 AlertDialog.Builder() 之前在代码中设置它,然后在 onClick() 中访问它,如下所示:
final int someParameter = someValue;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
alertDialogBuilder
.setTitle("Are you sure?")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do something with parameter.
doSomeStuff(someParameter);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
这样,someParameter 通过函数 closure 隐式传递给 onClick(),因此无需继承 AlertDialog 或向您的 Activity 添加额外的成员变量。
你也可以在应用中使用静态变量。定义 public 静态字符串 SumValue="";
假设我的调用 class 是 MainActivity Class 那么,我可以在调用对话框之前初始化变量,例如
MainActivity.SumValue="测试值"; addcartdialog addcartdialog = new addcartdialog(); addcartdialog.show( ((AppCompatActivity) 上下文).getSupportFragmentManager(),"添加购物车");
在添加购物车对话框中class
你可以分配给 textView = view.findViewById(R.id.servingsTxt); textView = MainActivity.SumValue;
通过这种方式,您可以在 alertdialog 中使用多个变量。