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

v64noz0r  于 2022-11-04  发布在  Android
关注(0)|答案(4)|浏览(178)

我已经找到了很多关于这个问题的参考资料,但还没有找到解决办法。
我使用下面的代码来隐藏虚拟键盘,但是它不起作用。

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的值始终为零,因此键盘代码不执行。为什么?

f5emj3cl

f5emj3cl1#

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

daupos2t2#

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

s6fujrry3#

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

yacmzcpb4#

只需将此用于 Delphi Alexandria,FMX:
对不起,伙计们,我不知道如何格式化这个。但是只要复制过去,自己格式化就行了。

相关问题