如何使用Delphi检测Android中的虚拟键盘事件

时间:2014-06-12 10:40:28

标签: android delphi keyboard keyboard-events

我正在尝试使用带有Delphi的Rad studio Xe5开发Android应用程序,我遇到以下问题:

有一个Tmemo,位于屏幕的底部,在按下它键入一些文本时,虚拟键盘显示在Tmemo上,我无法看到。我想在显示键盘上检测事件并移动更改该Tmemo的位置。当隐藏虚拟键盘时,我会做同样的事情,并将Tmemo恢复到原来的位置。有人知道在键盘上显示和隐藏事件会很热吗?

最好的问候

2 个答案:

答案 0 :(得分:1)

您可以使用它来隐藏键盘,

private InputMethodManager mKeyboard;
    mKeyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mKeyboard.hideSoftInputFromWindow(countryTo.getWindowToken(), 0);

答案 1 :(得分:0)

键盘上的tmemo遇到了同样的问题,请尝试这两个函数以及事件OnVirtualKeyboardShownOnVirtualKeyboardHidden

public
    { Public declarations }
    FSavedY: Single;
    FocusControl: TControl;
    ParentedControl: TFMXObject;
    function FocusedControl: TControl;
    function GetFocusedControlOffset(KeyboardRect: TRect): Single;

...

function TfrmFeedBackMobile.FocusedControl: TControl;
begin
  Result := nil;
  if Assigned(Focused) and (Focused.GetObject is TControl) then
    Result := TControl(Focused.GetObject);
end;

function TfrmFeedBackMobile.GetFocusedControlOffset(KeyboardRect: TRect): Single;
var
  Control: TControl;
  ControlPos: TPointF;
  KeyboardTop: Single;
begin
  Result := 0;
  KeyboardTop := Height - (KeyboardRect.Bottom - KeyboardRect.Top) - 66;
  // At least, should be. 66 is the height of the keyboard "done" bar
  Control := FocusedControl;
  if Assigned(Control) then
  begin
    ControlPos := Control.LocalToAbsolute(PointF(0, 0));
    Result := KeyboardTop - ControlPos.Y + Control.Height + 2;
    if Result >= 0 then
      Result := 0;
  end;
end;

procedure TfrmFeedbackMobile.FormVirtualKeyboardHidden(Sender: TObject;
  KeyboardVisible: Boolean; const Bounds: TRect);
begin
  FocusControl.Parent:= ParentedControl;
  FocusControl.AnimateFloat('Position.Y', FSavedY, 0.1);
  FocusControl.Align := TAlignLayout.alClient;
  FocusControl:= nil;
end;

procedure TfrmFeedbackMobile.FormVirtualKeyboardShown(Sender: TObject;
  KeyboardVisible: Boolean; const Bounds: TRect);
begin
  FocusControl:= FocusedControl;
  if not (FocusControl is TMemo) then Exit;

  FocusControl.Align := TAlignLayout.alNone;
  FSavedY := FocusControl.Position.Y;
  FocusControl.Position.Y:= 0;
  FocusControl.AnimateFloat('Position.Y',
    FSavedY + GetFocusedControlOffset(Bounds), 0.1);
  ParentedControl:= FocusControl.Parent;
  FocusControl.Parent:= frmFeedbackMobile;
  FocusControl.BringToFront;
end;