I've got an Activity with a bunch of EditTexts and a button. When the user clicks the button, an answer based on the input from the EditTexts appears in the caption of the button. In the buttonclick handler I change the focus to the button and the cursor disappears from whatever EditText has the focus, but the soft keyboard remains on the screen. How can I force the soft keyboard to vanish?
EditText1.ClearFocus();
EditText2.ClearFocus();
EditText3.ClearFocus();
calc_btn.Focusable = true;
calc_btn.RequestFocus();
I've seen several answers as to how to do this in Java, but I haven't been able to figure out how to translate them to C#.
Thanks for any advice.
You can do something like this:
var inputManager = (InputMethodManager)GetSystemService(InputMethodService);
inputManager.HideSoftInputFromWindow(editText.WindowToken, HideSoftInputFlags.None);
Jon O's answer above is perfect. This is how you use it.
Window.SetSoftInputMode(SoftInput.StateHidden);
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);
}
};
}
I have used the given methods in above code.I dont want to show the soft keypad for both the textboxs.Its not working,have i written it incorrectly?
Is this useful to you?
There seems to be a method here:
public override void HideSoftInput (int flags, Android.OS.ResultReceiver resultReceiver)
Here is a completely working solution:
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);
}
};