Delphi安卓 formclosequery事件问题

ZeaginXie 2019-08-10 04:22:30
var Check: Boolean; begin Check :=False; MessageDlg('确认退出吗?', System.UITypes.TMsgDlgType.mtInformation,[TMsgDlgBtn.mbYes,TMsgDlgBtn.mbn.mbCancel], 0, procedure(const AResult: TModalResult) begin if AResult = mrYES thenCheck :=FalTrueeCancel'); anClose :=Check; end; 同样的代码在Windows平台没什么问题,为什么在安卓平台无法关闭窗口?感觉直接跳过了MessageDlg,如果第一行改成Check :=True,又会直接关闭窗口。。。是哪里错了吗,该怎么写才能正常显示提示框并使得按钮有效呢?改过很多次,要么直接不显示消息框,要么显示了之后点yes还是no不正常,要么都不动,要么都会关闭窗口。麻烦大神们帮忙看看是怎么回事,谢谢! end); 直接将结果
...全文
152 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lao_yunger 2019-08-10
  • 打赏
  • 举报
回复
Platform Without ACloseDialogProc With ACloseDialogProc
Windows Blocking Blocking
OS X Blocking Blocking
iOS Blocking Non-blocking
Android Non-blocking
ZeaginXie 2019-08-10
  • 打赏
  • 举报
回复
引用 1 楼 qq_457565758 的回复:
TDialogService.MessageDialog('Choose a button:', System.UITypes.TMsgDlgType.mtInformation,
[System.UITypes.TMsgDlgBtn.mbYes, System.UITypes.TMsgDlgBtn.mbNo, System.UITypes.TMsgDlgBtn.mbCancel],
System.UITypes.TMsgDlgBtn.mbYes, 0,

// Use an anonymous method to make sure the acknowledgment appears as expected.
procedure(const AResult: TModalResult)
begin
case AResult of
{ Detect which button was pushed and show a different message }
mrYES:
//Check :=False;
//anClose :=Check;
ShowMessage('You chose Yes');


mrNo:
ShowMessage('You chose No');
mrCancel:
ShowMessage('You chose Cancel');
end;
end);
抱歉1楼发出来的时候不知怎的就乱掉了,有些字符删掉了不完整。回复楼上,我用了你发的代码,把他改成了:

TDialogService.MessageDialog('确认退出吗?',
System.UITypes.TMsgDlgType.mtInformation,
[System.UITypes.TMsgDlgBtn.mbYes, System.UITypes.TMsgDlgBtn.mbNo],
System.UITypes.TMsgDlgBtn.mbYes, 0,

// Use an anonymous method to make sure the acknowledgment appears as expected.
procedure(const AResult: TModalResult)
begin
case AResult of
{ Detect which button was pushed and show a different message }
mrYES:
begin
Check :=True;
ShowMessage('You chose Yes');
end;


mrNo:
ShowMessage('You chose No');
end;
end);

if Check =True then
begin
ShowMessage('You chose Yes');
CanClose :=True;
end;

但是问题来了,这段代码:

if Check =True then
begin
ShowMessage('You chose Yes');
CanClose :=True;
end;

本意是判断用户是否在前面的消息中选择了True,但我发现运行时程序会跳过 TDialogService.MessageDialog 直接关闭窗体,如果把 if Check =True then和下面的代码删掉,又可以显示消息框并且可以根据按钮进行不同的提示了。但是这样便没办法CanClose 关闭窗体了,不知道问题出在哪里,该如何解决呢
lao_yunger 2019-08-10
  • 打赏
  • 举报
回复
TDialogService.MessageDialog('Choose a button:', System.UITypes.TMsgDlgType.mtInformation,
[System.UITypes.TMsgDlgBtn.mbYes, System.UITypes.TMsgDlgBtn.mbNo, System.UITypes.TMsgDlgBtn.mbCancel],
System.UITypes.TMsgDlgBtn.mbYes, 0,

// Use an anonymous method to make sure the acknowledgment appears as expected.
procedure(const AResult: TModalResult)
begin
case AResult of
{ Detect which button was pushed and show a different message }
mrYES:
//Check :=False;
//anClose :=Check;
ShowMessage('You chose Yes');


mrNo:
ShowMessage('You chose No');
mrCancel:
ShowMessage('You chose Cancel');
end;
end);


ZeaginXie 2019-08-10
  • 打赏
  • 举报
回复
楼上的方法试过了,点yes的话消息框会闪一下(关闭后又再次弹出,不点no就不能退出),窗体依旧没办法关闭。网上的说法是安卓平台下的消息框都是异步模式的,即弹出消息后不等用户点击按钮就会继续执行下一句。所以我觉得如果把MessageDlg 放在CanClose := FCanClose后面的话可能会被直接跳过,所以改成这样:
定义全局变量 var FCanClose :Boolean = False;

FormCloseQuery事件代码:

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose :=FCanClose;

MessageDlg('确认退出吗?',
System.UITypes.TMsgDlgType.mtInformation,
[System.UITypes.TMsgDlgBtn.mbYes, System.UITypes.TMsgDlgBtn.mbNo], 0,
procedure(const AResult: TModalResult)
begin
if AResult = mrYES then
begin
FCanClose :=True;
Self.Close;
end;
end)
end;

问题基本解决(点击yes之后会再次弹出消息,不过只有一瞬间,窗体关闭)
lao_yunger 2019-08-10
  • 打赏
  • 举报
回复
var
FCanClose :boolean;
begin
FCanClose:=false;
if not FCanClose then
FMX.Dialogs.MessageDlg(
'Exit?',
TMsgDlgType.mtConfirmation,
[TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo],
0,
procedure(const AResult: TModalResult)
begin
if AResult = mrYes then
begin
FCanClose := True; // set the field value
Close; // call close again
end;
end );

CanClose := FCanClose;
end;

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧