Keyboard 如何在Xamarin窗体中关闭按键时的键盘
经过多次搜寻,我找到了一种方法,在iOS手机壳中,以Xamarin的形式将键盘隐藏在按键上。下面就分享一下Keyboard 如何在Xamarin窗体中关闭按键时的键盘,keyboard,xamarin.forms,Keyboard,Xamarin.forms,经过多次搜寻,我找到了一种方法,在iOS手机壳中,以Xamarin的形式将键盘隐藏在按键上。下面就分享一下 如果有人可以改进它,或者为Android端共享一个解决方案,那就太好了。在shared/PCL项目中,添加: using System; using Xamarin.Forms; namespace MyApp.Views { public class ButtonKeyboardHiding : Button {} } using System; using Xamarin.
如果有人可以改进它,或者为Android端共享一个解决方案,那就太好了。在shared/PCL项目中,添加:
using System;
using Xamarin.Forms;
namespace MyApp.Views
{
public class ButtonKeyboardHiding : Button {}
}
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 );
};
}
}
}
}
在表单中使用此类代替按钮
在iOS项目中,添加:
using System;
using Xamarin.Forms;
namespace MyApp.Views
{
public class ButtonKeyboardHiding : Button {}
}
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 );
};
}
}
}
}
使用系统;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.iOS;
使用基础;
使用UIKit;
使用MyApp.Views;
使用MyApp.iOS;
[程序集:ExportRenderer(typeof(ButtonkeBoardHiding)、typeof(ButtonkeBoardHidingRenderer))]
名称空间MyApp.iOS
{
公共类ButtonKeyBoardHidRender:ButtonRender
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
碱基。一个元素改变(e);
if(控件!=null)
{
Control.TouchUpInside+=(发送方,el)=>
{
UIView ctl=控制;
while(true)
{
ctl=ctl.Superview;
如果(ctl.Description.Contains(“UIView”))
打破
}
ctl.EndEditing(真);
};
}
}
}
}
这是我为Android端隐藏虚拟键盘的解决方案
我编写了一个IKeyboardInteractions接口,它有一个HideKeyboard()方法。然后我在MyProject.Droid中声明了一个KeyboardInteractions类,它实现了IKeyboardInteractions:
通用代码:
public interface IKeyboardInteractions {
void HideKeyboard();
}
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);
}
}
}
}
消费方式:
IKeyboardInteractions keyboardInteractions = DependencyService.Get<IKeyboardInteractions>();
keyboardInteractions.HideKeyboard ();
IKeyboardInteractions-keyboardInteractions=DependencyService.Get();
keyboardInteractions.HideKeyboard();
我发现这很有用:
界面:
public interface IKeyboardHelper
{
void HideKeyboard();
}
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();
}
}
}
DependencyService.Get<IKeyboardHelper>().HideKeyboard();
iOS:
public interface IKeyboardHelper
{
void HideKeyboard();
}
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();
}
}
}
DependencyService.Get<IKeyboardHelper>().HideKeyboard();
机器人:
public interface IKeyboardHelper
{
void HideKeyboard();
}
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();
}
}
}
DependencyService.Get<IKeyboardHelper>().HideKeyboard();
在Xamarin形式中的用法:
public interface IKeyboardHelper
{
void HideKeyboard();
}
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();
}
}
}
DependencyService.Get<IKeyboardHelper>().HideKeyboard();
DependencyService.Get().HideKeyboard();
以下是F.Badili基于Xamarin Forms 2.5中不推荐的更改而提出的解决方案的更新版本。唯一的区别是在Android类中如何访问上下文
在Xamarin表单项目中:
公共接口IKeyboardHelper
{
无效隐藏板();
}
在iOS项目中:
使用系统;
使用Xamarin.Forms;
使用ProjectName;
使用ProjectName.iOS;
使用UIKit;
[程序集:依赖项(typeof(iOSKeyboardHelper))]
名称空间ProjectName.iOS
{
公共类iOSKeyboardHelper:IKeyboardHelper
{
公共无效隐藏板()
{
UIApplication.SharedApplication.KeyWindow.EndEditing(true);
}
}
}
在Droid项目中:
使用系统;
使用Xamarin.Forms;
使用ProjectName;
使用ProjectName.Droid;
使用Xamarin.Forms.Platform.Android;
使用Android.Views.InputMethods;
使用Android.App;
使用Android.Content;
[程序集:Xamarin.Forms.Dependency(typeof(DroidKeyboardHelper))]
名称空间ProjectName.Droid
{
公共类DroidKeyboardHelper:IKeyboardHelper
{
公共无效隐藏板()
{
var context=Android.App.Application.context;
var inputMethodManager=context.GetSystemService(context.InputMethodService)作为inputMethodManager;
如果(inputMethodManager!=null&&上下文为活动)
{
var-activity=作为活动的上下文;
var token=activity.CurrentFocus?.WindowToken;
inputMethodManager.HideSoftInputFromWindow(令牌,HideSoftInputFlags.None);
activity.Window.DecorView.ClearFocus();
}
}
}
}
在Xamarin表单中第页:
DependencyService.Get().HideKeyboard();
即使有凯南·凯西的更新,我也无法让F.巴迪利的Android解决方案工作。进一步的谷歌搜索让我想到了对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();
}
}
}
}
protected override void OnCreate(Bundle savedInstanceState){
base.OnCreate(savedInstanceState);
...
KeyboardHelper.Init(this);
...
LoadApplication(new App());
}
在Droid MainActivity.cs中:
[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();
}
}
}
}
protected override void OnCreate(Bundle savedInstanceState){
base.OnCreate(savedInstanceState);
...
KeyboardHelper.Init(this);
...
LoadApplication(new App());
}
注意:
[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();
}
}
}
}
protected override void OnCreate(Bundle savedInstanceState){
base.OnCreate(savedInstanceState);
...
KeyboardHelper.Init(this);
...
LoadApplication(new App());
}
这仅在应用程序仅在单个活动上运行时有效。如果在任何情况下,您的应用程序都有多个活动,请参阅以获取适用于您的应用程序的正确实施
就这样
有些人说这不起作用,但对我来说,这就像是帮派杀手。如果移除焦点的方法对你不起作用(对我也不起作用),经过大量搜索后,我意识到一种简单的方法可以强制键盘关闭
FindViewById<EditText>(Resource.Id.edittextname).Enabled = false;
FindViewById<EditText>(Resource.Id.edittextname).Enabled = true;
findviewbyd(Resource.Id.edittextname).Enabled=false;
findviewbyd(Resource.Id.edittextname).Enabled=true;
就这样。只需禁用并启用它,它就会关闭键盘。Forms9Patch具有良好的工作实现 因此,只需使用整个库()或使用Github repo()中的代码即可 因此,使用它非常容易:
Forms9Patch.KeyboardService.Hide();
适用于Android、iOS和UWP。Xamarin Forms 4.8背后代码的简单解决方案。订阅TextChanged事件,即编辑器或条目,然后检查代码隐藏中的密钥。正如Le Mot Juice所回答的,UnFocused()将关闭键盘。我的示例在按下enter键后关闭键盘,但您可以对条件应用另一个键 XAML
<Editor TextChanged="Input_TextChanged"></Editor>
无法在ListView上迭代视图似乎很可笑。谢谢,这个主意。我试过了,但没用。当我第二次点击按钮时(键盘关闭后),我得到了一个例外。非常感谢Nicholas。我们将尝试一下