2012-04-03 113 views
2

我有一堆EditTexts和一个按钮的活动。当用户点击该按钮时,在该按钮的标题中出现基于来自EditTexts的输入的回答。在buttonclick处理程序中,我将焦点更改为按钮,并且光标从EditText具有的焦点中消失,但软键盘仍保留在屏幕上。我怎样才能强制软键盘消失?隐藏MonoDroid中的软键盘

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

我见过好几个答案至于如何做到这一点在Java中,但我一直无法弄清楚如何将它们转换为C#。

感谢您的任何建议。

回答

3

你可以做这样的事情:以上

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

完美的作品,格雷格。非常感谢。 – 2012-04-03 22:48:25

+0

不知道为什么这不适合我:( – PCoder 2013-01-03 09:14:14

1

this对您有用吗?

似乎是这里的方法:

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

+0

我敢肯定,如果我能弄清楚如何调用它,那么这个方法将非常有用。这不会产生该方法,所以它似乎不直接实现:Android.InputMethodServices.InputMethodService.InputMethodImpl。 – 2012-04-03 22:46:52

+0

不要忘记接受正确的答案! :) – 2012-04-04 00:21:55

2

Jon O的答案是完美的。这是你如何使用它。

Window.SetSoftInputMode(SoftInput.StateHidden); 
0

这里是一个完全工作液:

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); 
      } 
     }; 
2
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); 
      } 
     }; 



    } 

我用上面code.I给定的方法不想要显示的软键盘的两个textboxs.Its不工作,我写错了吗?