Android 键盘在页面渲染器中失去对触摸的关注 [英] Android keyboard loses focus on touch in Page Renderer
问题描述
我有一个带有自定义键盘的应用.为此,我使用了页面渲染器,因此我可以使用 Android 布局,这使键盘逻辑更易于处理.
I have an app with a custom keyboard. To do this, I use a page renderer so I can use an Android layout which makes the keyboard logic a lot easier to handle.
当我在键盘上长按 (.5s) 时,一切正常.当我快速点击某个键时(大多数人都会这样做),键盘所针对的 EditText
失去焦点,导致键盘隐藏并且光标从 EditText代码>.我设置了一个
FocusChanged
处理程序来在用户点击屏幕上的其他地方时隐藏键盘.由于此错误,当我在键盘点击后执行 FindFocus
时,焦点返回为 null
.我显示了布局边界,以防某些幽灵视图接收到点击,但那里什么也没有.我不知道键盘事件的生命周期是什么,但是在 mKeyboardView.Key
之后调用了导致此问题的任何原因.
When I do a longish press (.5s) on the keyboard, everything works great. When I do a quick tap on a key (what most people do), the EditText
targeted by the keyboard loses focus, causing the keyboard to hide and the cursor to be removed from the EditText
. I set up a FocusChanged
handler to hide the keyboard when the user clicks elsewhere on the screen. With this error, when I do FindFocus
after a keyboard tap, the focus returns as null
. I showed layout bounds in case of some ghost view receiving the click, but there's nothing there. I don't know what the lifecycle is on a keyboard event, but whatever is causing this issue is called after 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);
}
}
可以在此处找到整个项目.iOS 项目运行良好,如果有人也想实现它.
Whole project can be found here. iOS project is working pretty well if anyone is looking to implement that as well.
推荐答案
删除 EditText.FocusChange
方法并像这样修改你的 CustomKeyboardView.Key
方法:
Delete the EditText.FocusChange
method and modify your CustomKeyboardView.Key
method like this :
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);
};
效果.
这是我的解决方法:
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();
};
然后,光标将始终可见.
Then, the cursor will always be visible.
这篇关于Android 键盘在页面渲染器中失去对触摸的关注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!