You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi FireMonkey for Android中MessageDlg的正确用法咨询

Delphi FireMonkey for Android 正确实现弹窗的方法

在FireMonkey的Android平台上,直接调用同步版MessageDlg会因阻塞UI线程引发错误,必须使用异步方式实现弹窗交互。以下是两种正确的实现方案:

方案一:使用TDialogServiceAsync(推荐)

这是FireMonkey专为移动平台设计的异步弹窗服务,不会阻塞主线程,代码示例:

uses
  FMX.DialogService;

procedure ShowDeleteUserConfirmDialog;
begin
  TDialogServiceAsync.MessageDialog('delete user', TMsgDlgType.mtWarning,
    [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], TMsgDlgBtn.mbNo, 0,
    procedure(const AResult: TModalResult)
    begin
      if AResult = mrYes then
      begin
        // 这里编写删除用户的逻辑
      end
      else
      begin
        // 用户取消删除,执行对应逻辑
      end;
    end);
end;

方案二:使用FMX版MessageDialog异步重载

如果习惯类似VCL的写法,可直接调用FMX.Dialogs单元中的异步重载MessageDialog:

uses
  FMX.Dialogs;

procedure ShowDeleteUserConfirmDialog;
begin
  MessageDialog('delete user', mtWarning, [mbYes, mbNo], mbNo,
    procedure(const AResult: TModalResult)
    begin
      if AResult = mrYes then
      begin
        // 处理用户确认删除的逻辑
      end;
    end);
end;

关键注意事项

  • 确保代码引用的是FMX.Dialogs单元,而非VCL的Vcl.Dialogs,避免混淆API。
  • 移动平台绝对禁止使用无回调的同步弹窗(即原代码MessageDlg('unknown', mtWarning, [mbOK], 0)这种写法),会导致UI线程阻塞,引发应用无响应或崩溃。
  • 回调函数内避免执行耗时操作,如需处理复杂逻辑,应放到后台线程中执行。

内容的提问来源于stack exchange,提问作者frank frank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 18:42:03