我有一个带自定义键盘的应用。为此,我使用页面渲染器,因此我可以使用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项目工作得非常好。
答案 0 :(得分:1)
删除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);
};
以下是我的解决方法:
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();
};
然后,光标将始终可见。