【问题标题】:Xamarin.Forms Entry That Should Not Show SoftKeyboard on Its Focus不应在其焦点上显示软键盘的 Xamarin.Forms 条目
【发布时间】:2019-04-28 15:19:04
【问题描述】:

我正在开发 Xamarin Forms 项目,我正在使用条目视图,这是要求,因为我需要能够专注于它,但也要求不显示软键盘。

此要求无法更改。另外,我不能使用 Label 或 Button 代替 Entry,因为它们没有获得焦点。

在阅读这篇文章 (https://theconfuzedsourcecode.wordpress.com/2017/05/19/a-keyboard-disabled-entry-control-in-xamarin-forms/comment-page-1/#comment-1300) 之后,我尝试创建自定义渲染器并在 Android 上使用 ShowSoftInputOnFocus,但这会短暂显示,然后隐藏软键盘。这不是一个选项,因为我的要求是严格不能在此自定义输入字段上显示软键盘。

所以,我在 Xamarin.Forms 项目(.NET Standard 2.0)中创建了我的自定义 KeyboardlessEntry

namespace MyProjNamespace
{
    public class KeyboardlessEntry : Entry
    {
    }
}

,然后是我在 Xamarin.Droid 头部项目中的自定义 KeyboardlessEntryRenderer 渲染器,如下所示:

[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 Focus 上,然后消失,因为我在OnPropertyChanging 事件中调用了HideSoftInputFromWindow

我不确定为什么这不起作用。我希望像上面那样将 ShowSoftInputOnFocus 设置为 false 会完全禁用软键盘。有人声称这适用于 Android 或 Xamarin.Android,但不适用于 Xamarin.Forms。

iOS 上的类似问题,这里是 iOS 的渲染器

[程序集:ExportRenderer(typeof(KeyboardlessEntry), typeof(KeyboardlessEntryRenderer))] 命名空间 Squirrel.FoH.App.iOS.Implementations.Controls { 公共类 KeyboardlessEntryRenderer : EntryRenderer { 公共KeyboardlessEntryRenderer() { }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        this.Control.InputView = new UIView(); // disable soft keyboard
    }
}

}

这也会显示,然后将键盘缩小到下方,但请注意没有按钮可以完全关闭它,这更加烦人

【问题讨论】:

  • 你能解释一下你想用这个实现什么吗?拥有和输入白色键盘显示没有意义。
  • 您可以使用标签来达到目的!如果您无论如何都不允许编辑,为什么需要条目!
  • 以粗体显示更新。
  • @NirmalSubedi 嘿,你是对的,我不会编辑它。该条目实际上是隐藏的,它用于接收扫描的输入。它连接到一个命令,然后触发以处理扫描的输入。您将如何使用标签来做到这一点?
  • @cd491415 你有没有找到阻止软键盘的解决方案?

标签: android ios xamarin.forms soft-keyboard


【解决方案1】:

尝试在你的 KeyboardlessEntryRenderer 类中添加这个:

public KeyboardlessEntryRenderer (Context context) : base(context)
{
}

【讨论】:

  • 你能解释一下你的答案吗?这将如何隐藏键盘?
  • @iamlawrencev 这不会阻止键盘显示。它只是删除了警告,因为最新版本的 Xamarin.Forms 需要 Android 的渲染器才能接收 Context 对象。所以,我更新以反映这一点,谢谢分享。但这与问题无关。
猜你喜欢
  • 2017-10-23
  • 2011-11-09
  • 2011-11-18
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 2019-04-25
  • 2019-06-22
  • 1970-01-01
相关资源
最近更新 更多