简体   繁体   English

如何处理(Delphi)Android应用中的后退按钮按下操作?

[英]How do I handle a back-button press in a (Delphi) Android app?

How can I make my Android app react to the back-button? 如何使我的Android应用对后退按钮做出反应?

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? 是否可以使用高级VCL的TApplicationEvents来处理它,还是我需要在这里深入研究低级Android专用的东西?

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. 按下psysical按钮似乎总是会退出应用程序,在某些情况下会导致访问冲突。

In the form's OnKey... events, the Key parameter is vkHardwareBack on Android. 在表单的OnKey...事件中, Key参数在Android上为vkHardwareBack 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;

Here is an updated code for Remy's answer (works with Seattle): 这是雷米答案的更新代码(与Seattle一起使用):

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 (TVirtualKeyboardState.Visible in FService.VirtualKeyBoardState) then
    begin
      // Back button pressed, keyboard visible, so do nothing...
    end else
    begin
      Key := 0;
      // Back button pressed, keyboard not visible or not supported on this platform, lets exit the app...
      MessageDlg('Exit Application?', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbOK, TMsgDlgBtn.mbCancel], -1, OnCloseDialog);
    end;
  end;
end;

procedure TForm1.OnCloseDialog(Sender: TObject; const AResult: TModalResult);
begin
  if AResult = mrOK then
    Close;
end;

For future reference to anyone trying to make sense of this.. 供以后尝试参考的人参考。

if Key = vkHardwareBack then
    begin
      // your code here
      key := 0;
end;

The key := 0; 键:= 0; is the secret to stop the app from closing.. 是阻止应用程序关闭的秘密。

This goes in the forms OnKeyUp event 这以OnKeyUp事件的形式进行

Going back to the previous screen depends on your application design. 返回上一个屏幕取决于您的应用程序设计。

  • If you used TTabControl for displaying pages, you can navigate to the previous TTabItem . 如果使用TTabControl来显示页面,则可以导航到上一个TTabItem

  • If you used TForms for displaying pages, you must use Close() procedure for closing the current form and going back to the previous screen. 如果使用TForms显示页面,则必须使用Close()过程关闭当前表单并返回上一屏幕。

Try this: 尝试这个:

uses FMX.Platform,FMX.VirtualKeyboard,FMX.Helpers.Android;

procedure THeaderFooterForm.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
          if MessageDlg('Exit Application?', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbOK, TMsgDlgBtn.mbCancel], -1) = mrOK then
            begin
            // Exit application here...
              SharedActivity.Finish;
            end;
        end;
     end
  else
    // Menu button pressed
    if Key = sgiUpRightLong then
      begin
        showmessage('Menu button pressed');
      end;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM