How to detect Virtual Keyboard Events in Android with Delphi

落爺英雄遲暮 提交于 2019-12-24 05:38:09

问题


I'm trying to develop an Android app with Rad studio Xe5 with Delphi, and i having the following problem:

There is a Tmemo, which is at the bottom of the screen, and at the time of pressing it for typing some text, the virtual keyboard is showed over the Tmemo which i can't see. I would like to detect the event on show keyboard and move change the position of that Tmemo. I will do the same when the Virtual Keyboard is hide, and bring back the Tmemo to his original position. Does anybody know hot to detect the event on keyboard show and hide?

Best Regards


回答1:


You can use this to hide the keyboard,

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



回答2:


I had the same problem with the keyboard over tmemo, try these two functions and the events OnVirtualKeyboardShown and OnVirtualKeyboardHidden

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;


来源:https://stackoverflow.com/questions/24182353/how-to-detect-virtual-keyboard-events-in-android-with-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!