簡體   English   中英

如何處理(Delphi)Android應用中的后退按鈕按下操作?

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

如何使我的Android應用對后退按鈕做出反應?

是否可以使用高級VCL的TApplicationEvents來處理它,還是我需要在這里深入研究低級Android專用的東西?

現在,大多數演示應用程序都有一個屏幕上的后退按鈕,可以返回到先前的屏幕。 按下psysical按鈕似乎總是會退出應用程序,在某些情況下會導致訪問沖突。

在表單的OnKey...事件中, Key參數在Android上為vkHardwareBack 例如:

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;

這是雷米答案的更新代碼(與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;

供以后嘗試參考的人參考。

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

鍵:= 0; 是阻止應用程序關閉的秘密。

這以OnKeyUp事件的形式進行

返回上一個屏幕取決於您的應用程序設計。

  • 如果使用TTabControl來顯示頁面,則可以導航到上一個TTabItem

  • 如果使用TForms顯示頁面,則必須使用Close()過程關閉當前表單並返回上一屏幕。

嘗試這個:

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