Android键盘在Page Renderer

时间:2017-11-14 16:10:18

标签: xamarin xamarin.android

我有一个带自定义键盘的应用。为此,我使用页面渲染器,因此我可以使用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项目工作得非常好。

1 个答案:

答案 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);
};

Effect

更新:

以下是我的解决方法:

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