繁体   English   中英

使用 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.0 到 10.3.1 中运行的 Android 应用程序中的代码

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;

我目前在柏林 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 应用程序中看到的“再次按下退出”功能。 为了使其正常工作,您还必须执行以下操作:

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM