使用 Delphi 10.3 在 android 应用程序中隐藏虚拟键盘
Hide virtual keyboard in android application with Delphi 10.3
我找到了很多关于这个问题的参考资料,但我还没有找到解决方案。
我使用下面的代码来隐藏虚拟键盘,但是没有用。
FService: IFMXVirtualKeyboardService;
...
procedure TForm1.FormCreate(Sender: TObject);
begin
TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService, IInterface(FService));
if FService = nil then ShowMessage('xxxxx');
end;
.....
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
//ShowMessage(IntToStr(Key) + '~' + KeyChar + '~');
//Application.ProcessMessages;
if (Key = vkHardwareBack) then
begin
// this code is executed
Application.Terminate;
Key := 0;
end
else
if Key in [vkRETURN, vkACCEPT] then begin
// this code never executed
if (FService <> nil) then begin // FService isn't nil
FService.HideVirtualKeyboard;
end;
end;
end;
当"Accept"或"Enter"被按下时,Key
的值始终为零,所以键盘代码不会被执行。为什么?
我目前在柏林 10.1 工作,所以我认为它应该可以,我调用了一个程序
Keyboard: IFMXVirtualKeyboardService;
procedure CallForKeyboard(open: Boolean; input: TFmxObject);
begin
if open then
begin
Keyboard.ShowVirtualKeyboard(input);
end
else
begin
if TVirtualKeyBoardState.Visible in Keyboard.GetVirtualKeyBoardState then
Keyboard.HideVirtualKeyboard;
end;
end;
当我想打开我调用的虚拟键盘时:
CallForKeyboard(true, sender)
如果我想关闭我调用的键盘:
CallForKeyboard(false,nil)
使用 FormkeyUp
事件处理程序:
procedure TfrmAppMain.FormKeyUp(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
begin
{$ifdef ANDROID}
if Key = vkHardwareBack then
begin
if FKeyBoardShown or KeyBoardVisible then // it lies
begin
// allow default behaviour - which hides the keyboard
// note: keyboardvisible also returns true on readonly fields
if (Self.Focused is TEdit) and TEdit(Self.Focused).ReadOnly then
begin
FToast.MakeToast('Press again to exit');
FBackPressed := True;
end;
end
else
begin
Key := 0; // NOTE: intercept default behaviour (which is to close the app)
if FBackPressed then
begin
SaveDataandClose; // which then calls Self.Close later
end
else
begin
FToast.MakeToast('Press again to exit');
FBackPressed := True;
end
end;
end;
{$endif}
end;
此代码还模拟了您在许多 Android 应用程序中看到的 "press again to exit" 功能。要使其正常工作,您还必须这样做:
procedure TfrmAppMain.FormTouch(Sender: TObject; const Touches: TTouches;
const Action: TTouchAction);
begin
FBackPressed := False; // as soon as they touch the form, the exit flag is reset
end;
这是我的 Android 应用程序的代码,这些应用程序一直在 10.0 到 10.3.1
中运行
procedure TfrmAppMain.FormKeyDown(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
begin
{$ifdef ANDROID}
// make enter like tab which shifts focus to the next control
// and may cause the keyboard to disappear and reappear in quick succession
// depending on the .killfocusbyreturn property of the current control
if Key = vkReturn then
begin
Key := vkTab;
KeyDown(Key, KeyChar, Shift);
end;
{$endif}
end;
我找到了很多关于这个问题的参考资料,但我还没有找到解决方案。
我使用下面的代码来隐藏虚拟键盘,但是没有用。
FService: IFMXVirtualKeyboardService;
...
procedure TForm1.FormCreate(Sender: TObject);
begin
TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService, IInterface(FService));
if FService = nil then ShowMessage('xxxxx');
end;
.....
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
//ShowMessage(IntToStr(Key) + '~' + KeyChar + '~');
//Application.ProcessMessages;
if (Key = vkHardwareBack) then
begin
// this code is executed
Application.Terminate;
Key := 0;
end
else
if Key in [vkRETURN, vkACCEPT] then begin
// this code never executed
if (FService <> nil) then begin // FService isn't nil
FService.HideVirtualKeyboard;
end;
end;
end;
当"Accept"或"Enter"被按下时,Key
的值始终为零,所以键盘代码不会被执行。为什么?
我目前在柏林 10.1 工作,所以我认为它应该可以,我调用了一个程序
Keyboard: IFMXVirtualKeyboardService;
procedure CallForKeyboard(open: Boolean; input: TFmxObject);
begin
if open then
begin
Keyboard.ShowVirtualKeyboard(input);
end
else
begin
if TVirtualKeyBoardState.Visible in Keyboard.GetVirtualKeyBoardState then
Keyboard.HideVirtualKeyboard;
end;
end;
当我想打开我调用的虚拟键盘时:
CallForKeyboard(true, sender)
如果我想关闭我调用的键盘:
CallForKeyboard(false,nil)
使用 FormkeyUp
事件处理程序:
procedure TfrmAppMain.FormKeyUp(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
begin
{$ifdef ANDROID}
if Key = vkHardwareBack then
begin
if FKeyBoardShown or KeyBoardVisible then // it lies
begin
// allow default behaviour - which hides the keyboard
// note: keyboardvisible also returns true on readonly fields
if (Self.Focused is TEdit) and TEdit(Self.Focused).ReadOnly then
begin
FToast.MakeToast('Press again to exit');
FBackPressed := True;
end;
end
else
begin
Key := 0; // NOTE: intercept default behaviour (which is to close the app)
if FBackPressed then
begin
SaveDataandClose; // which then calls Self.Close later
end
else
begin
FToast.MakeToast('Press again to exit');
FBackPressed := True;
end
end;
end;
{$endif}
end;
此代码还模拟了您在许多 Android 应用程序中看到的 "press again to exit" 功能。要使其正常工作,您还必须这样做:
procedure TfrmAppMain.FormTouch(Sender: TObject; const Touches: TTouches;
const Action: TTouchAction);
begin
FBackPressed := False; // as soon as they touch the form, the exit flag is reset
end;
这是我的 Android 应用程序的代码,这些应用程序一直在 10.0 到 10.3.1
中运行procedure TfrmAppMain.FormKeyDown(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
begin
{$ifdef ANDROID}
// make enter like tab which shifts focus to the next control
// and may cause the keyboard to disappear and reappear in quick succession
// depending on the .killfocusbyreturn property of the current control
if Key = vkReturn then
begin
Key := vkTab;
KeyDown(Key, KeyChar, Shift);
end;
{$endif}
end;