经过大量搜索,我找到了一种在 Xamarin Forms for iOS 中按下按钮时隐藏键盘的方法。所以分享如下。
如果有人可以改进它,或者分享 Android 端的解决方案,那就太好了。
最佳答案
我发现这很有用:
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();
关于keyboard - 如何在 Xamarin Forms 中按下按钮时关闭键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30437362/