Dismiss keyboard when click outside of EditText in android

后端 未结 9 1111
旧时难觅i
旧时难觅i 2021-02-01 05:31

I have an EditText called myTextview. I want the soft keyboard to show when I click on the EditText but then dismiss if I click outside of

9条回答
  •  情书的邮戳
    2021-02-01 05:43

    For all those who are looking for a Xamarin Android code for this :

      public override bool DispatchTouchEvent(MotionEvent ev)
        {
            try
            {
                View view = CurrentFocus;
                if (view != null && (ev.Action == MotionEventActions.Up || ev.Action == MotionEventActions.Move) && view is EditText && !view.Class.Name.StartsWith("android.webkit."))
                {
                    int[] Touch = new int[2];
                    view.GetLocationOnScreen(Touch);
                    float x = ev.RawX + view.Left - Touch[0];
                    float y = ev.RawY + view.Top - Touch[1];
                    if (x < view.Left || x > view.Right || y < view.Top || y > view.Bottom)
                        ((InputMethodManager)GetSystemService(InputMethodService)).HideSoftInputFromWindow((Window.DecorView.ApplicationWindowToken), 0);
                }
            }
            catch (System.Exception ex)
            {
    
            }
    
            return base.DispatchTouchEvent(ev);
        }
    

提交回复
热议问题