我想创建一个与C#或vb.net相当的函数msgbox
msgbox返回True或False。如果用户单击是,则返回true,否则返回false。
我尝试编码如下:
public boolean msgbox(String msg, String title )
{
Boolean isclicked =false;
if (title == null)
{
title = apinf.name; // If Title is null, then Set App Name as Title
}
AlertDialog.Builder add = new AlertDialog.Builder(ctx);
add.setTitle(title);
add.setMessage(msg);
add.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
isclicked = true;
}
});
add.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
isclicked=false;
}
});
add.show();
return isclicked;
}
我将使用它:
if ( msgbox("Are you sure to close ?") == true)
{
// My code to do
}
有没有办法实现这个目标?