android dialog 传参数,Android:将参数传递给Alert Dialog
如果您将变量声明为final,那么您可以在调用AlertDialog.Builder()之前在代码中设置它,然后在onClick()中访问它,如下所示:final int someParameter = someValue;AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);alertDialogBuilde
如果您将变量声明为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添加额外的成员变量.
更多推荐
所有评论(0)