在C# Xamarin android中自定义键盘

mi7gmzs6  于 2022-12-31  发布在  C#
关注(0)|答案(2)|浏览(260)

我做了一个自定义的数字键盘,我想隐藏系统键盘,我怎样才能在xamarin c#中做到呢?
我做了一个自定义的键盘使用,我想禁用系统键盘的好。我想知道,我怎么才能做到这一点在xamarin android c#

2ic8powd

2ic8powd1#

首先,您可以重写方法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);
        }

其次,可以创建一个类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);
            }
        }
    }

然后可以像这样使用类。

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);
        }
72qzrwbm

72qzrwbm2#

非常感谢,我忘了提一下,我只想以一种形式使用数字键盘,而不是每种形式。

相关问题