Android键盘在Page Renderer中失去了对触摸的关注

实践是知识的母亲,知识是生活的明灯。这篇文章主要讲述Android键盘在Page Renderer中失去了对触摸的关注相关的知识,希望能为你提供帮助。
【Android键盘在Page Renderer中失去了对触摸的关注】我有一个带自定义键盘的应用程序。为此,我使用页面渲染器,因此我可以使用android布局,使键盘逻辑更容易处理。
当我在键盘上进行长按(.5s)时,一切都很好。当我快速点击一个键(大多数人都这么做)时,键盘所针对的EditText失去焦点,导致键盘隐藏,光标从EditText中移除。当用户点击屏幕上的其他位置时,我设置了一个FocusChanged处理程序来隐藏键盘。有了这个错误,当我在键盘敲击后执行FindFocus时,焦点将返回null。我在一些鬼视图接收到点击时显示了布局边界,但那里什么也没有。我不知道键盘事件的生命周期是什么,但是在mKeyboardView.Key之后调用导致此问题的任何问题。
有任何想法吗?谢谢。

class KeyboardPageRenderer : PageRenderer {public CustomKeyboardView mKeyboardView; public EditText mTargetView; public Android.InputMethodServices.Keyboard mKeyboard; Activity activity; global::Android.Views.View view; protected override void OnElementChanged(ElementChangedEventArgs< Page> e) { base.OnElementChanged(e); if (e.OldElement != null || Element == null) { return; }try { SetupUserInterface(); SetupEventHandlers(); this.AddView(view); } catch (System.Exception ex) { System.Diagnostics.Debug.WriteLine(@"ERROR: ", ex.Message); } }void SetupUserInterface() { activity = this.Context as Activity; view = activity.LayoutInflater.Inflate(Resource.Layout.Main, this, false); mKeyboard = new Android.InputMethodServices.Keyboard(Context, Resource.Xml.keyboard); mTargetView = view.FindViewById< EditText> (Resource.Id.target); mKeyboardView = view.FindViewById< CustomKeyboardView> (Resource.Id.keyboard_view); mKeyboardView.Keyboard = mKeyboard; }void SetupEventHandlers() { mTargetView.Touch += (sender, e) => { ShowKeyboardWithAnimation(); e.Handled = false; mTargetView.ShowSoftInputOnFocus = false; }; mTargetView.FocusChange += (sender, e) => { var idk = FindFocus(); if (!mTargetView.IsFocused) { mKeyboardView.Visibility = ViewStates.Gone; }}; mKeyboardView.Key += (sender, e) => { long eventTime = javaSystem.CurrentTimeMillis(); KeyEvent ev = new KeyEvent(eventTime, eventTime, KeyEventActions.Down, e.PrimaryCode, 0, 0, 0, 0, KeyEventFlags.SoftKeyboard | KeyEventFlags.KeepTouchMode); DispatchKeyEvent(ev); }; }public void ShowKeyboardWithAnimation() { if (mKeyboardView.Visibility == ViewStates.Gone) { mKeyboardView.Visibility = ViewStates.Visible; Android.Views.Animations.Animation animation = AnimationUtils.LoadAnimation( Context, Resource.Animation.slide_up_bottom ); mKeyboardView.ShowWithAnimation(animation); } }protected override void OnLayout (bool changed, int l, int t, int r, int b) { base.OnLayout (changed, l, t, r, b); var msw = MeasureSpec.MakeMeasureSpec (r - l, MeasureSpecMode.Exactly); var msh = MeasureSpec.MakeMeasureSpec (b - t, MeasureSpecMode.Exactly); view.Measure (msw, msh); view.Layout (0, 0, r - l, b - t); } }

编辑:
整个项目可以找到here。如果有人想要实现它,ios项目工作得很好。
答案删除EditText.FocusChange方法并修改你的CustomKeyboardView.Key方法,如下所示:
mKeyboardView.Key += (sender, e) => { long eventTime = JavaSystem.CurrentTimeMillis(); KeyEvent ev = new KeyEvent(eventTime, eventTime, KeyEventActions.Down, e.PrimaryCode, 0, 0, 0, 0, KeyEventFlags.SoftKeyboard | KeyEventFlags.KeepTouchMode); //Make your editText get Focus mTargetView.RequestFocus(); DispatchKeyEvent(ev); };

Effect。
Update :这是我的解决方法:
mKeyboardView.Key += async (sender, e) => { long eventTime = JavaSystem.CurrentTimeMillis(); KeyEvent ev = new KeyEvent(eventTime, eventTime, KeyEventActions.Down, e.PrimaryCode, 0, 0, 0, 0, KeyEventFlags.SoftKeyboard | KeyEventFlags.KeepTouchMode); DispatchKeyEvent(ev); await Task.Delay(1); //mTargetView.RequestFocus(); };

然后,光标将始终可见。
Effect like this。

    推荐阅读