Скройте виртуальную клавиатуру в приложении для Android с Delphi 10.3 - PullRequest
1 голос
/ 20 марта 2019

Я нашел много ссылок на эту проблему, но пока не нашел решения.

Я использую следующий код, чтобы скрыть виртуальную клавиатуру, но она не работает.

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 ]

1 голос
/ 20 марта 2019

Это код из моих приложений для 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;
0 голосов
/ 20 марта 2019

Используйте обработчик событий 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;
0 голосов
/ 20 марта 2019

Я сейчас работаю в 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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...