16

如何让我的 Android 应用程序对返回按钮做出反应?

是否有高级 VCL 的 TApplicationEvents 来处理它,还是我需要在这里深入研究低级 Android 特定的东西?

现在,大多数演示应用程序都有一个屏幕后退按钮,可以返回到前一个屏幕。按下心理按钮似乎总是退出应用程序,在某些情况下会导致访问冲突。

4

6 回答 6

37

在表单的OnKey...事件中,Key参数vkHardwareBack在 Android 上。例如:

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;
于 2013-09-12T21:39:44.537 回答
10

供任何试图理解这一点的人将来参考。

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

密钥:= 0;是阻止应用程序关闭的秘密..

这以 OnKeyUp 事件的形式出现

于 2017-09-30T20:39:22.667 回答
8

这是 Remy 答案的更新代码(与西雅图合作):

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;
于 2015-08-18T09:22:57.787 回答
2

返回上一个屏幕取决于您的应用程序设计。

  • 如果您用于TTabControl显示页面,则可以导航到上一个TTabItem.

  • 如果您用于TForms显示页面,则必须使用Close()关闭当前表单并返回上一个屏幕的过程。

于 2013-09-14T22:41:38.423 回答
1

尝试这个:

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;
于 2013-12-11T18:15:19.487 回答
1

GetIt Package Manager ( https://getitnow.embarcadero.com/card-view-wizard/ ) 提供的 Card View Wizard 1.0 具有此代码(我只更改了普通的“// do nothing comment”以更具描述性)仅当一组启动卡序列仍然或返回第一张卡时退出应用程序,否则导航到上一张卡:

procedure TMainForm.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
        if WizardTabControl.ActiveTab <> TabItem1 then
          begin
            WizardTabControl.SetActiveTabWithTransitionAsync(WizardTabControl.Tabs[WizardTabControl.TabIndex-1],TTabTransition.Slide,TTabTransitionDirection.Reversed,nil);
            Key := 0;
          end;
      end;
  end
end;

当然,正如其他答案所示,您也可以提示用户,以防他们不小心尝试退出应用程序,不要继续。

于 2021-09-20T15:43:43.217 回答