2012-04-03 114 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不工作,我寫錯了嗎?