Android键盘在Page Renderer中失去了对触摸的关注
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}
}
编辑:
答案
删除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);
};
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();
};
然后,光标将始终可见。
以上是关于Android键盘在Page Renderer中失去了对触摸的关注的主要内容,如果未能解决你的问题,请参考以下文章
如何从内部文件路径 data/data/com.xyz/file.pdf 在 android 5.0 的 Pdf Renderer 中打开 pdf