如何在 Xamarin Forms 中按下按钮时关闭键盘 [英] How to dismiss keyboard on button press in Xamarin Forms
本文介绍了如何在 Xamarin Forms 中按下按钮时关闭键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
经过多次寻找,我找到了一种方法,可以在 Xamarin Forms 中的按钮按下时隐藏键盘,适用于 iOS 案例.所以在下面分享.
After much hunting I worked out a way to hide the keyboard on a button press in Xamarin Forms, for the iOS case. So it's shared below.
如果有人可以改进它,或者分享 Android 方面的解决方案,那就太好了.
If anyone can improve it, or share a solution for the Android side, that would be great.
推荐答案
我觉得这很有用:
https://forums.xamarin.com/discussion/comment/172077#Comment_172077
界面:
public interface IKeyboardHelper
{
void HideKeyboard();
}
iOS:
public class iOSKeyboardHelper : IKeyboardHelper
{
public void HideKeyboard()
{
UIApplication.SharedApplication.KeyWindow.EndEditing(true);
}
}
机器人:
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();
}
}
}
在 Xamarin 表单中的使用:
DependencyService.Get<IKeyboardHelper>().HideKeyboard();
这篇关于如何在 Xamarin Forms 中按下按钮时关闭键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文