【问题标题】:Close/hide the Android Soft Keyboard in MvxFragment在 MvxFragment 中关闭/隐藏 Android 软键盘
【发布时间】:2015-09-04 09:02:06
【问题描述】:

我使用 xamarin + mvvmcross 创建 android 应用程序。我的 MvxFragment 中有一个 MvxAutoCompleteTextView。在 MvxAutoCompleteTextView 中写入并单击其他控件后,我想隐藏虚拟键盘。我用这个代码

public class MyFragment : MvxFragment 
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState)
    {

        base.OnCreateView(inflater, container, savedInstanceState);
        var view = this.BindingInflate(Resource.Layout.frMy, null);
        var autoComplete = view.FindViewById<MvxAutoCompleteTextView>(Resource.Id.acMy);
        InputMethodManager inputManager = (InputMethodManager)inflater.Context.GetSystemService(Context.InputMethodService);
        inputManager.HideSoftInputFromWindow(autoComplete.WindowToken, HideSoftInputFlags.None);
        return view;
    }
}

但这不起作用。如何隐藏键盘?

【问题讨论】:

    标签: android xamarin mvvmcross android-softkeyboard


    【解决方案1】:

    我找到的最简单的解决方案是简单地调用具有焦点的控件上的Unfocus() 方法(例如EntryEditor)。如果要直接在 XAML 中指定函数,可以将此调用嵌入到 UnfocusOnClicked 行为中。

    【讨论】:

      【解决方案2】:

      我从这里得到了 java 的答案:https://stackoverflow.com/a/28939113/4664754

      这是 C# 版本(经过测试和工作):

      public override bool DispatchTouchEvent(MotionEvent ev)
      {
          if (ev.Action == MotionEventActions.Down)
          {
              View v = CurrentFocus;
              if (v.GetType() == typeof(EditText))
              {
                  Rect outRect = new Rect();
                  v.GetGlobalVisibleRect(outRect);
                  if (!outRect.Contains((int)ev.RawX, (int)ev.RawY))
                  {
                      v.ClearFocus();
                      InputMethodManager imm = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
                      imm.HideSoftInputFromWindow(v.WindowToken, 0);
                  }
              }
          }
          return base.DispatchTouchEvent(ev);
      }
      

      如果您单击任何不是 EditText 的内容,这段代码将隐藏软键盘。 您只需将其粘贴到您的活动类中(例如您的 loginActivity)

      【讨论】:

        【解决方案3】:

        试试这个:

        InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
        imm.HideSoftInputFromWindow(autoComplete.WindowToken, 0);
        

        HideSoftInputFromWindow 中的值 0 是 const Android.Views.InputMethods.HideSoftInputFlags.None,因此您可以使用等效的语法:

        InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
        imm.HideSoftInputFromWindow(autoComplete.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
        

        【讨论】:

          【解决方案4】:

          您可以隐藏软键盘,将焦点放在非“键盘启动器”控件上,例如自动完成控件的父容器。

          parentContainer = FindViewById<LinearLayout>(Resource.Id.parentContainer);
          parentContainer.RequestFocus();
          

          假设您的父容器是一个 LinearLayout,您应该允许它通过以下 2 个属性获得焦点:

          <LinearLayout
              android:id="@+id/parentContainer"
              android:focusable="true"
              android:focusableInTouchMode="true">
          

          【讨论】:

            【解决方案5】:

            试试我的功能:

            public static void Close_AndroidKeyboard(Activity context){
                InputMethodManager inputManager = (InputMethodManager) context.getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
            

            对不起,我在 android studio 工作,如果我帮助了你并且编程很好,请告诉我!

            【讨论】:

              猜你喜欢
              • 2014-07-19
              • 2017-06-07
              • 2012-01-21
              • 2013-12-09
              • 1970-01-01
              • 2022-12-14
              • 2014-07-23
              • 2011-12-24
              • 1970-01-01
              相关资源
              最近更新 更多