使用Delphi 10.3在Android应用程序中隐藏虚拟键盘

时间:2019-03-20 01:15:25

标签: android delphi keyboard

我找到了很多有关此问题的参考,但是我还没有找到解决方法。

我使用以下代码隐藏虚拟键盘,但是它不起作用。

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;

当按下“接受”或“输入”时,Key的值始终为零,因此不会执行键盘代码。为什么?

3 个答案:

答案 0 :(得分:1)

这是我的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;

答案 1 :(得分:0)

我目前在柏林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)

答案 2 :(得分:0)

使用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;