Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在(Delphi)Android应用程序中处理后退按钮按下?_Android_Delphi_Back Button_Delphi Xe5 - Fatal编程技术网

如何在(Delphi)Android应用程序中处理后退按钮按下?

如何在(Delphi)Android应用程序中处理后退按钮按下?,android,delphi,back-button,delphi-xe5,Android,Delphi,Back Button,Delphi Xe5,如何让我的Android应用程序对后退按钮做出反应 有没有像高级VCL的TApplicationEvents这样的东西来处理它,或者我需要在这里深入研究低级Android特定的东西 现在,大多数演示应用程序都有一个屏幕上的后退按钮,可以返回到上一个屏幕。按下psysical按钮似乎总是会退出应用程序,在某些情况下会导致访问冲突。在表单的OnKey…事件中,Android上的Key参数是vhardwareback。例如: uses FMX.Platform, FMX.VirtualKeyboa

如何让我的Android应用程序对后退按钮做出反应

有没有像高级VCL的TApplicationEvents这样的东西来处理它,或者我需要在这里深入研究低级Android特定的东西


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

在表单的
OnKey…
事件中,Android上的
Key
参数是
vhardwareback
。例如:

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;
使用
FMX.Platform,FMX.VirtualKeyboard;
过程TForm1.FormKeyUp(发送方:TObject;变量关键字:Word;变量KeyChar:Char;Shift:TShiftState);
变量
FService:IFMXVirtualKeyboardService;
开始
如果Key=vk,则返回
开始
TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService,IInterface(fsservice));
如果(FService nil)和(vksVisible在FService.VirtualKeyBoardState中),则
开始
//按下后退按钮,键盘可见,所以什么也不做。。。
结束其他
开始
//按下后退按钮,键盘不可见或此平台不支持,退出应用程序。。。
如果MessageDlg('Exit Application?',TMsgDlgType.mtConfirmation,[TMsgDlgBtn.mbOK,TMsgDlgBtn.mbCancel],-1)=mrOK,则
开始
//在此退出应用程序。。。
结束其他
开始
//他们改变了主意,所以忽略按下后退按钮。。。
键:=0;
结束;
结束;
结束
...
结束;

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

  • 如果使用
    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;
使用FMX.Platform、FMX.VirtualKeyboard、FMX.Helpers.Android;
过程THeaderFooterForm.FormKeyUp(发送者:ToObject;变量关键字:Word;
var KeyChar:Char;Shift:TShiftState);
var-FService:IFMXVirtualKeyboardService;
开始
如果Key=vk,则返回
开始
TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService,IInterface(fsservice));
如果(FService nil)和(vksVisible在FService.VirtualKeyBoardState中),则
开始
//按下后退按钮,键盘可见,所以什么也不做。。。
结束
其他的
开始
如果MessageDlg('Exit Application?',TMsgDlgType.mtConfirmation,[TMsgDlgBtn.mbOK,TMsgDlgBtn.mbCancel],-1)=mrOK,则
开始
//在此退出应用程序。。。
SharedActivity.Finish;
结束;
结束;
结束
其他的
//按下菜单按钮
如果Key=sgiUpRightLong,则
开始
显示消息(“按下菜单按钮”);
结束;
结束;

以下是雷米答案的更新代码(与西雅图合作):

过程TForm1.FormKeyUp(发送方:TObject;变量关键字:Word;变量KeyChar:Char;移位:TShiftState);
变量
FService:IFMXVirtualKeyboardService;
开始
如果Key=vk,则返回
开始
TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService,IInterface(fsservice));
如果(FService nil)和(TVirtualKeyboardState.Visible in FService.VirtualKeyBoardState),则
开始
//按下后退按钮,键盘可见,所以什么也不做。。。
结束其他
开始
键:=0;
//按下后退按钮,键盘不可见或此平台不支持,退出应用程序。。。
MessageDlg('Exit Application?',TMsgDlgType.mtConfirmation,[TMsgDlgBtn.mbOK,TMsgDlgBtn.mbCancel],-1,OnCloseDialog);
结束;
结束;
结束;
过程TForm1.OnCloseDialog(发送方:ToObject;常量结果:TModalResult);
开始
如果AResult=mrOK,则
接近;
结束;

以供任何试图理解这一点的人参考

if Key = vkHardwareBack then
    begin
      // your code here
      key := 0;
end;
键:=0;是阻止应用关闭的秘密


这可以在表单OnKeyUp事件中找到

有关在上使用vkeyeback的更多注释。该页面提到了本示例中使用的技术:根据我的测试,在当前版本的Delphi(XE5)上工作的唯一方法是将键设置为:=0;即使用户按下“确定,退出”。FormKeyUp被调用两次(我不知道为什么),第二次显示消息DLG(试图显示)时,应用程序关闭,因此整个应用程序挂起,无法重新启动。在XE6和更高版本中,
vksVisible
被替换为.Remy,您可以根据我的回答更新代码。使用XE9测试,不,他不能更新他的答案,因为OP要求使用XE5^^^,但是@LU RD在XE6中提供了唯一需要的修改(至少对于我使用XE7…),之后,
vksVisible
被替换为。没有XE9。XE8之后的下一个版本是10西雅图。@RemyLebeau谢谢。我想当我发布这篇文章时,没有正式的名字。
if Key = vkHardwareBack then
    begin
      // your code here
      key := 0;
end;