如何让我的Android应用对后退按钮做出反应?
VCL的TApplicationEvents是否有高级处理能力,或者我是否需要深入研究低级别的Android特定内容?
目前,大多数演示应用程序都有一个屏幕后退按钮,可以返回上一个屏幕。按下psysical按钮似乎总是退出应用程序,在某些情况下会导致访问冲突。
答案 0 :(得分:33)
在表单的OnKey...
个事件中,Android上的Key
参数为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;
答案 1 :(得分:6)
以下是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;
答案 2 :(得分:6)
以备任何想要了解这一点的人参考。
if Key = vkHardwareBack then
begin
// your code here
key := 0;
end;
关键:= 0;是阻止应用关闭的秘诀..
这是OnKeyUp事件的形式
答案 3 :(得分: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;
答案 4 :(得分:0)
返回上一个屏幕取决于您的应用程序设计。
如果您使用TTabControl
来显示网页,则可以导航到之前的TTabItem
。
如果您使用TForms
来显示页面,则必须使用Close()
程序关闭当前表单并返回上一屏幕。