【问题标题】:Hiding the soft keyboard in MonoDroid在 MonoDroid 中隐藏软键盘
【发布时间】:2012-04-03 20:23:14
【问题描述】:

我有一个带有一堆 EditText 和一个按钮的 Activity。当用户单击按钮时,基于 EditTexts 输入的答案会出现在按钮的标题中。在 buttonclick 处理程序中,我将焦点更改为按钮,光标从 EditText 具有焦点的任何内容中消失,但软键盘仍保留在屏幕上。如何强制软键盘消失?

EditText1.ClearFocus();    
EditText2.ClearFocus();    
EditText3.ClearFocus();
calc_btn.Focusable  =   true;
calc_btn.RequestFocus();

我已经看到了几个关于如何在 Java 中执行此操作的答案,但我无法弄清楚如何将它们翻译成 C#。

感谢您的建议。

【问题讨论】:

    标签: android xamarin.android android-softkeyboard


    【解决方案1】:

    你可以这样做:

    var inputManager = (InputMethodManager)GetSystemService(InputMethodService);
    inputManager.HideSoftInputFromWindow(editText.WindowToken, HideSoftInputFlags.None);
    

    【讨论】:

      【解决方案2】:

      Jon O 上面的回答是完美的。这就是你使用它的方式。

      Window.SetSoftInputMode(SoftInput.StateHidden);
      

      【讨论】:

        【解决方案3】:
        protected override void OnCreate (Bundle bundle)
            {
                base.OnCreate (bundle);
        
                SetContentView (Resource.Layout.Main);
        
                Button button = FindViewById<Button> (Resource.Id.button1);
                button.Click+= onClick;
                EditText es=FindViewById<EditText>(Resource.Id.editText1);
                es.EditorAction += (object sender, TextView.EditorActionEventArgs e) => 
                {
        
                    if ( e.ActionId == Android.Views.InputMethods.ImeAction.Done )
                    {
                        var editText = sender as EditText;
                        var inputManager = GetSystemService(InputMethodService) as InputMethodManager;
                        inputManager.HideSoftInputFromWindow(editText.WindowToken, 0);
                    }
        
                };
                EditText es1=FindViewById<EditText>(Resource.Id.editText2);
                es1.EditorAction += (object sender, TextView.EditorActionEventArgs e) => 
                {
                    if ( e.ActionId == Android.Views.InputMethods.ImeAction.Done )
                    {
                        var editText = sender as EditText;
                        var inputManager = GetSystemService(InputMethodService) as InputMethodManager;
                        inputManager.HideSoftInputFromWindow(editText.WindowToken, 0);
                    }
                };
        
        
        
            }
        

        我在上面的代码中使用了给定的方法。我不想显示两个文本框的软键盘。它不工作,我写错了吗?

        【讨论】:

          【解决方案4】:

          this 对你有用吗?

          这里好像有个方法:

          public override void HideSoftInput (int flags, Android.OS.ResultReceiver resultReceiver)

          【讨论】:

          • 如果我能弄清楚如何调用它,我相信该方法会很有用。这不会产生方法,所以它似乎没有直接实现:Android.InputMethodServices.InputMethodService.InputMethodImpl.
          【解决方案5】:

          这是一个完全可行的解决方案:

          using Android.Views.InputMethods;
          
          yourEditTextObject.EditorAction += (object sender, TextView.EditorActionEventArgs e) => 
                  {
                      if ( e.ActionId == Android.Views.InputMethods.ImeAction.Done )
                      {
                          var editText = sender as EditText;
                          var inputManager = GetSystemService(InputMethodService) as InputMethodManager;
                          inputManager.HideSoftInputFromWindow(editText.WindowToken, 0);
                      }
                  };
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-08-29
            • 1970-01-01
            • 1970-01-01
            • 2022-12-14
            • 2023-01-09
            • 2011-12-24
            • 1970-01-01
            相关资源
            最近更新 更多