使用IFMXVirtualKeyboardService隐藏虚拟键盘后,虚拟键盘将丢失

时间:2015-02-19 12:20:39

标签: delphi firemonkey delphi-xe7

我的表单上有TEditTTMSFMXWebGMaps。在我的编辑OnKeyUp()事件中,我有这段代码来隐藏iPhone 4的虚拟键盘:

if TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService) then
      (TPlatformServices.Current.GetPlatformService(IFMXVirtualKeyboardService) as IFMXVirtualKeyboardService).HideVirtualKeyboard;

问题是,如果不将焦点更改为其他控件,我无法再次显示键盘。我在我的编辑OnTap()中尝试了这个,但它没有带回键盘:

  if TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService) then
    (TPlatformServices.Current.GetPlatformService(IFMXVirtualKeyboardService) as IFMXVirtualKeyboardService).ShowVirtualKeyboard(edSearch);

由于我的表单只包含一个TEdit,因此除非用户导航到另一个表单并返回,否则键盘将永远丢失。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

隐藏虚拟键盘有一种非常简单的方法。

uses
{$IFDEF IOS}
  FMX.Forms
{$ENDIF}
{$IFDEF Android}
  Androidapi.JNI.Embarcadero,
  FMX.Platform.Android,
  FMX.Helpers.Android
{$ENDIF};

procedure HideVirtualKeyboard;
{$IFDEF IOS}
begin
  try
    Screen.ActiveForm.Focused := nil;
  except
  end;
end;
{$ENDIF}
{$IFDEF Android}    
var
  TextView: JFMXTextEditorProxy;
begin
  try
    begin
      TextView := MainActivity.getTextEditorProxy;
      CallInUIThread(
        procedure
        begin
          TextView.setFocusable(false);
          TextView.setFocusableInTouchMode(false);
          TextView.showSoftInput(false);
          TextView.clearFocus;
          TextView.setFocusable(true);
          TextView.setFocusableInTouchMode(true);
        end);
    end
  except
  end;
end;
{$ENDIF}