[英]Custom Keyboard in c# Xamarin android
我做了一个自定义数字键盘,我想隐藏系统键盘,在xamarin c# 中怎么办?
我制作了一个自定义键盘来使用,我想永久禁用系统键盘。 我想知道,我怎样才能在 xamarin.android c#
首先,您可以重写OnTouchEvent
方法以关闭设备上的键盘。
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);
}
二、可以创建一个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);
}
}
}
然后你可以像这样使用 class。
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);
}
非常感谢,我忘了说我只想用一种形式使用数字键盘,而不是每种形式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.