Xamarin.Forms条目不应在其焦点上显示SoftKeyboard [英] Xamarin.Forms Entry That Should Not Show SoftKeyboard on Its Focus
问题描述
我正在处理Xamarin Forms项目,并且正在使用Entry视图,这是必需条件,因为我需要能够专注于该视图,但是这也要求不显示软键盘.
I am working on Xamarin Forms project and I am using Entry view and that is requirement since i need to be able to focus on it but it is also requirement to not show soft keyboard.
此要求不能更改.另外,我无法使用标签"或按钮"代替输入",因为它们没有获得焦点.
Following this article (https://theconfuzedsourcecode.wordpress.com/2017/05/19/a-keyboard-disabled-entry-control-in-xamarin-forms/comment-page-1/#comment-1300), I tried creating custom renderer and using ShowSoftInputOnFocus on Android but that will briefly show, then hide soft keyboard. This is not an option since my requirement is strict to not show soft keyboard on this custom Entry field at all.
因此,我在Xamarin.Forms项目(.NET Standard 2.0)中创建了自定义KeyboardlessEntry
:
So, I created my custom KeyboardlessEntry
in Xamarin.Forms project (.NET Standard 2.0):
namespace MyProjNamespace
{
public class KeyboardlessEntry : Entry
{
}
}
,然后在Xamarin.Droid head项目中使用我的自定义KeyboardlessEntryRenderer
渲染器,如下所示:
, and then my custom KeyboardlessEntryRenderer
renderer in Xamarin.Droid head project like so:
[assembly: ExportRenderer(typeof(KeyboardlessEntry), typeof(KeyboardlessEntryRenderer))]
namespace MyProjNamespace.Droid
{
public class KeyboardlessEntryRenderer : EntryRenderer
{
//as of latest Xamarin.Forms need to provide c-tor that
//receives Android Context and sets it in base
public KeyboardlessEntryRenderer (Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
((KeyboardlessEntry)e.NewElement).PropertyChanging += OnPropertyChanging;
}
if (e.OldElement != null)
{
((KeyboardlessEntry)e.OldElement).PropertyChanging -= OnPropertyChanging;
}
this.Control.ShowSoftInputOnFocus = false; // disable soft keyboard on focus
}
private void OnPropertyChanging(object sender, PropertyChangingEventArgs propertyChangingEventArgs)
{
if (propertyChangingEventArgs.PropertyName == VisualElement.IsFocusedProperty.PropertyName)
{
// fully dismiss the soft keyboard
InputMethodManager imm = (InputMethodManager)this.Context.GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(this.Control.WindowToken, 0);
}
}
}
}
如您所见,我在OnElementChanged
替代中设置了ShowSoftInputOnFocus
,但这并不能由于某些原因阻止它显示.我的键盘仍然显示在KeyboardlessEntry
焦点上,然后消失了,因为我在OnPropertyChanging
事件中调用了HideSoftInputFromWindow
.
As you can see, I am setting ShowSoftInputOnFocus
in OnElementChanged
override but that does not prevent it from showing for some reason. My keyboard still shows on KeyboardlessEntry
Focus and then disappears because I call HideSoftInputFromWindow
inside OnPropertyChanging
event.
我不确定为什么这行不通.我希望像上面一样将ShowSoftInputOnFocus
设置为false会导致软键盘无法完全显示.有人声称这可以在Android或Xamarin.Android上使用,但在Xamarin.Forms中不可用.
I am not sure why this is not working. I would expect that setting ShowSoftInputOnFocus
to false like I do above would disable soft keyboard from showing entirely. Some people claim that this works on Android or Xamarin.Android but it does not work in Xamarin.Forms.
iOS上的类似问题,这是iOS的渲染器
Similar issue on iOS, here is the renderer for iOS
[程序集:ExportRenderer(typeof(KeyboardlessEntry),typeof(KeyboardlessEntryRenderer))] 命名空间Squirrel.FoH.App.iOS.Implementations.Controls { 公共类KeyboardlessEntryRenderer:EntryRenderer { 公共KeyboardlessEntryRenderer() { }
[assembly: ExportRenderer(typeof(KeyboardlessEntry), typeof(KeyboardlessEntryRenderer))] namespace Squirrel.FoH.App.iOS.Implementations.Controls { public class KeyboardlessEntryRenderer : EntryRenderer { public KeyboardlessEntryRenderer() { }
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
this.Control.InputView = new UIView(); // disable soft keyboard
}
}
}
这也会显示,然后将键盘缩小到下方,但请注意,没有按钮可以完全关闭它,这更令人烦恼
This also shows, then reduces keyboard to strip below but note that there is no button to close it entirely making it even more annoying
推荐答案
尝试将其添加到您的KeyboardlessEntryRenderer
类中:
Try adding this inside your KeyboardlessEntryRenderer
class:
public KeyboardlessEntryRenderer (Context context) : base(context)
{
}
这篇关于Xamarin.Forms条目不应在其焦点上显示SoftKeyboard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!