我该如何处理后退按钮preSS的(德尔福)Android应用程序?我该、如何处理、应用程序、按钮

2023-09-06 01:47:59 作者:秃头小宝贝

我怎样才能让我的Andr​​oid应用程序反应后退按钮?

How can I make my Android app react to the back-button?

有什么作为高层次的VCL的TApplicationEvents来处理它,或者我需要潜水深入到这里的低级别的Andr​​oid特有的东西?

Is there something as high-level VCL's TApplicationEvents to handle it, or do I need to dive deep into low-level Android-specific stuff here?

目前,大多数的演示应用程序有一个屏幕上的后退按钮返回到previous屏幕。 $ P $总是pssing的psysical按钮似乎退出程序,而在某些情况下,它会导致访问冲突

Right now, most of the demo applications have an on-screen back button to go back to a previous screen. Pressing the psysical button always seems to quit the app, and in some situations it results in an access violation.

推荐答案

在表单的 OnKey ... 活动时,参数 vkHardwareBack 在Android上。例如:

In the form's OnKey... events, the Key parameter is vkHardwareBack on Android. For example:

uses
  FMX.Platform, FMX.VirtualKeyboard;

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
var
  FService : IFMXVirtualKeyboardService;
begin
  if Key = vkHardwareBack then
  begin
    TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService, IInterface(FService));
    if (FService <> nil) and (vksVisible in FService.VirtualKeyBoardState) then
    begin
      // Back button pressed, keyboard visible, so do nothing...
    end else
    begin
      // Back button pressed, keyboard not visible or not supported on this platform, lets exit the app...
      if MessageDlg('Exit Application?', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbOK, TMsgDlgBtn.mbCancel], -1) = mrOK then
      begin
        // Exit application here...
      end else
      begin
        // They changed their mind, so ignore the Back button press...
        Key := 0;
      end;
    end;
  end
  ...
end;