繁体   English   中英

在 MonoDroid 中隐藏软键盘

[英]Hiding the soft keyboard in MonoDroid

我有一个带有一堆 EditText 和一个按钮的活动。 当用户单击按钮时,基于 EditTexts 输入的答案将出现在按钮的标题中。 在 buttonclick 处理程序中,我将焦点更改为按钮,并且 cursor 从具有焦点的任何 EditText 中消失,但软键盘仍保留在屏幕上。 如何强制软键盘消失?

EditText1.ClearFocus();    
EditText2.ClearFocus();    
EditText3.ClearFocus();
calc_btn.Focusable  =   true;
calc_btn.RequestFocus();

我在 Java 中看到了几个关于如何做到这一点的答案,但我一直无法弄清楚如何将它们翻译成 C#。

感谢您的任何建议。

你可以这样做:

var inputManager = (InputMethodManager)GetSystemService(InputMethodService);
inputManager.HideSoftInputFromWindow(editText.WindowToken, HideSoftInputFlags.None);

Jon O上面的回答是完美的。 这就是您使用它的方式。

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



    }

我在上面的代码中使用了给定的方法。我不想为两个文本框显示软键盘。它不起作用,我写错了吗?

对你有用吗?

这里似乎有一个方法:

public override void HideSoftInput (int flags, Android.OS.ResultReceiver resultReceiver)

这是一个完全有效的解决方案:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM