如何在Xamarin Forms中按下按钮上的键盘

Bil*_*llF 2 keyboard xamarin.forms

经过多次狩猎后,我找到了一种方法,可以在Xamarin Forms的按钮上隐藏键盘,适用于iOS机箱.所以它在下面分享.

如果有人可以改进它,或者为Android方共享解决方案,那就太好了.

小智 16

我觉得这很有用:

https://forums.xamarin.com/discussion/comment/172077#Comment_172077

接口:

public interface IKeyboardHelper
{
    void HideKeyboard();
}
Run Code Online (Sandbox Code Playgroud)

iOS版:

public class iOSKeyboardHelper : IKeyboardHelper
{
    public void HideKeyboard()
    {
        UIApplication.SharedApplication.KeyWindow.EndEditing(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

机器人:

public class DroidKeyboardHelper : IKeyboardHelper
{
    public void HideKeyboard()
    {
        var context = Forms.Context;
        var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
        if (inputMethodManager != null && context is Activity)
        {
            var activity = context as Activity;
            var token = activity.CurrentFocus?.WindowToken;
            inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);

            activity.Window.DecorView.ClearFocus();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在Xamarin表单中的用法:

DependencyService.Get<IKeyboardHelper>().HideKeyboard();
Run Code Online (Sandbox Code Playgroud)

  • 在每个KeyboardHelper类的命名空间开始之前你需要这一行,否则DependencyService就找不到它们.[assembly:Xamarin.Forms.Dependency(typeof(xxxxxxKeyboardHelper))] (5认同)

Le *_*ced 8

 view.Unfocus();
Run Code Online (Sandbox Code Playgroud)

就是这样。

有些人报告这不起作用,但它对我来说就像大片一样。


Ale*_*lex 6

如果经过大量搜索后移除焦点方法对您不起作用(并且对我不起作用),我发现了一种强制关闭键盘的简单方法。

FindViewById<EditText>(Resource.Id.edittextname).Enabled = false;
FindViewById<EditText>(Resource.Id.edittextname).Enabled = true;
Run Code Online (Sandbox Code Playgroud)

就是这样。只需禁用并启用它,它就会关闭键盘。

  • `MyEntry.IsEnabled=false` 比 `MyEntry.UnFocus()` 效果更好。 (3认同)

Nic*_*ert 5

这是我隐藏Android端虚拟键盘的解决方案.

我编写了一个具有HideKeyboard()方法的IKeyboardInteractions接口.然后我在MyProject.Droid中声明了一个实现IKeyboardInteractions的KeyboardInteractions类:

常用代码:

public interface IKeyboardInteractions {
    void HideKeyboard();
}
Run Code Online (Sandbox Code Playgroud)

MyProject.Droid代码:

[assembly: Dependency (typeof (KeyboardInteractions))] namespace MyProject.Droid
{
    public class KeyboardInteractions : IKeyboardInteractions
    {
        public void HideKeyboard()
        {
            var inputMethodManager = Xamarin.Forms.Forms.Context.GetSystemService(Context.InputMethodService) as InputMethodManager;
            if (inputMethodManager != null && Xamarin.Forms.Forms.Context is Activity)
            {
                var activity = Xamarin.Forms.Forms.Context as Activity;
                var token = activity.CurrentFocus == null ? null : activity.CurrentFocus.WindowToken;
                inputMethodManager.HideSoftInputFromWindow(token, 0);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

消费方法:

IKeyboardInteractions keyboardInteractions = DependencyService.Get<IKeyboardInteractions>();
keyboardInteractions.HideKeyboard ();
Run Code Online (Sandbox Code Playgroud)


Ken*_*sey 5

这是基于 Xamarin Forms 2.5 中弃用更改的 F. Badili 解决方案的更新版本。唯一的区别是如何在 Android 类中访问 Context。

Xamarin Forms项目中:

public interface IKeyboardHelper
{
    void HideKeyboard();
}   
Run Code Online (Sandbox Code Playgroud)

iOS项目中:

using System;
using Xamarin.Forms;
using ProjectName;
using ProjectName.iOS;
using UIKit;

[assembly: Dependency(typeof(iOSKeyboardHelper))]
namespace ProjectName.iOS
{
    public class iOSKeyboardHelper : IKeyboardHelper
    {
        public void HideKeyboard()
        {
            UIApplication.SharedApplication.KeyWindow.EndEditing(true);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

机器人项目中:

using System;
using Xamarin.Forms;
using ProjectName;
using ProjectName.Droid;
using Xamarin.Forms.Platform.Android;
using Android.Views.InputMethods;
using Android.App;
using Android.Content;

[assembly: Xamarin.Forms.Dependency(typeof(DroidKeyboardHelper))] 
namespace ProjectName.Droid
{
    public class DroidKeyboardHelper : IKeyboardHelper
    {
        public void HideKeyboard()
        {
            var context = Android.App.Application.Context;
            var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
            if (inputMethodManager != null && context is Activity)
            {
                var activity = context as Activity;
                var token = activity.CurrentFocus?.WindowToken;
                inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);

                activity.Window.DecorView.ClearFocus();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Xamarin 表单页面中:

DependencyService.Get<IKeyboardHelper>().HideKeyboard();
Run Code Online (Sandbox Code Playgroud)


Jul*_*tol 5

即使使用 Kenan Casey 的更新,我也无法让 F. Badili 的 Android 解决方案正常工作。进一步的谷歌搜索将我带到了Dave 的技术博客,了解Xamarin.Forms.Forms.Context. 它说Android.App.Application.Context不是并且无法转换为Activty(至少就我而言,请参阅博客文章的评论以获取解释)。

在Droid 项目中:

[assembly: Xamarin.Forms.Dependency(typeof(DroidKeyboardHelper))] 
namespace ProjectName.Droid{
    public class DroidKeyboardHelper : IKeyboardHelper {
        static Context _context;

        public static void Init(Context context) => _context = context;

        public void HideKeyboard(){
            var inputMethodManager = _context.GetSystemService(Context.InputMethodService) as InputMethodManager;
            if (inputMethodManager != null && _context is Activity) {
                var activity = _context as Activity;
                var token = activity.CurrentFocus?.WindowToken;
                inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);

                activity.Window.DecorView.ClearFocus();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在Droid MainActivity.cs中:

protected override void OnCreate(Bundle savedInstanceState){
    base.OnCreate(savedInstanceState);
    ...
    KeyboardHelper.Init(this);
    ...
    LoadApplication(new App());
}
Run Code Online (Sandbox Code Playgroud)

笔记

仅当您的应用程序仅在单个 Activity 上运行时,这才有效。如果您的应用程序有多个活动,请参阅Dave 的技术博客,了解适合您的案例的正确实施方式。


Bil*_*llF 2

在共享/PCL项目中,添加:

using System;
using Xamarin.Forms;

namespace MyApp.Views
{
    public class ButtonKeyboardHiding : Button {}
}
Run Code Online (Sandbox Code Playgroud)

在表单中使用此类代替 Button。

在iOS项目中添加:

using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using Foundation;
using UIKit;
using MyApp.Views;
using MyApp.iOS;

[assembly: ExportRenderer ( typeof (ButtonKeyboardHiding), typeof (ButtonKeyboardHidingRenderer ) ) ]

namespace MyApp.iOS
{
    public class ButtonKeyboardHidingRenderer : ButtonRenderer
    {
        protected override void OnElementChanged ( ElementChangedEventArgs<Button> e )
        {
            base.OnElementChanged  (e );

            if ( Control != null ) 
            {   
                Control.TouchUpInside += ( sender, el ) =>
                {
                    UIView ctl = Control;
                    while ( true )
                    {
                        ctl = ctl.Superview;
                        if ( ctl.Description.Contains ( "UIView" ) )
                            break;
                    }
                    ctl.EndEditing ( true );
                };
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)