I made a custom digital keyboard, I want to hide the system keyboard, how can I do it in xamarin c#?
I made a custom keyboard to use, and I want to disable the system keyboard for good. I would like to know, how can I do it in xamarin android c#
CodePudding user response:
First, you can rewrite the method OnTouchEvent
to close the keyboard on the device.
public override bool OnTouchEvent(MotionEvent e)
{
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
EditText Etusername = FindViewById<EditText>(Resource.Id.Etname);
imm.HideSoftInputFromWindow(Etusername.WindowToken, 0);
return base.OnTouchEvent(e);
}
Second, you can create a class HideAndShowKeyboard.cs
internal class HideAndShowKeyboard
{
/*
* Shows the soft keyboard
*/
public void showSoftKeyboard(Android.App.Activity activity, View view)
{
InputMethodManager inputMethodManager = (InputMethodManager)activity.GetSystemService(Context.InputMethodService);
view.RequestFocus();
inputMethodManager.ShowSoftInput(view, 0);
}
/*
* Hides the soft keyboard
*/
public void hideSoftKeyboard(Android.App.Activity activity)
{
var currentFocus = activity.CurrentFocus;
if (currentFocus != null)
{
InputMethodManager inputMethodManager = (InputMethodManager)activity.GetSystemService(Context.InputMethodService);
inputMethodManager.HideSoftInputFromWindow(currentFocus.WindowToken, HideSoftInputFlags.None);
}
}
}
Then you can use the class like this.
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
EditText Etusername = FindViewById<EditText>(Resource.Id.Etusername);
var hideAndShowKeyboard = new HideAndShowKeyboard();
hideAndShowKeyboard.showSoftKeyboard(this, Etusername);
}
CodePudding user response:
Thank you very much, I forgot to mention that I only want to use the digital keyboard in one form, not in each form.