【问题标题】:How can I disable Voice to Text button(Dictation button) on Keyboard in Xamarin Forms?如何在 Xamarin 表单中禁用键盘上的语音转文本按钮(听写按钮)?
【发布时间】:2016-12-03 10:46:11
【问题描述】:

我想在 Android 和 iOS 的 Xamarin 表单中禁用键盘上的语音转文本按钮。

我的问题是,我是否需要实现自定义渲染,因为我找不到适用于 Android 和 iOS 的通用代码。

我在下面找到了在本机 iOS 中实现的链接

Disable Dictation button on the keyboard of iPhone 4S / new iPad

任何建议将不胜感激。您还可以建议我可以在 Xamarin Forms 中复制的本机平台方法

谢谢

【问题讨论】:

  • 您需要使用自定义渲染器,因为键盘是特定于平台的。
  • 感谢您的回复,是的,我做到了,我已经创建了自定义渲染器,它适用于具有 PrivateImeOptions="nm" 属性的 android,现在正在尝试适用于 iOS,我不确定我是哪个条目属性应该用来禁用听写按钮

标签: android ios xamarin.ios xamarin.android xamarin.forms


【解决方案1】:

最后我通过为文本框和文本区域(编辑器)创建自定义渲染实现了这一点,下面的代码对我有用。
在表单中,我创建了这个类。

using Xamarin.Forms;

namespace CustomRenderer
{
    public class MyEntry : Entry
    {
    }
}

在 android 中,我为 MyEntry 创建了自定义渲染。

using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using CustomRenderer;
using CustomRenderer.Android;

[assembly: ExportRenderer (typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomRenderer.Android
{
    class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged (e);

            if (Control != null) {
                Control.SetBackgroundColor (global::Android.Graphics.Color.LightGreen);
                Control.PrivateImeOptions = "nm";
            }
        }
    }
}

iOS 下的渲染图。

using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using UIKit;
using CustomRenderer;
using CustomRenderer.iOS;

[assembly: ExportRenderer (typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomRenderer.iOS
{
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged (e);

            if (Control != null) {
                // do whatever you want to the UITextField here!
                Control.BackgroundColor = UIColor.FromRGB (204, 153, 255);
                Control.BorderStyle = UITextBorderStyle.Line;
                Control.KeyboardType = UIKeyboardType.EmailAddress; 
            }
        }
    }
}

在 xaml 中,我有以下语法。

<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:CustomRenderer;assembly=CustomRenderer"
    x:Class="CustomRenderer.MainPageXaml">
    <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
        <Label Text="Hello, Custom Renderer!" />
        <local:MyEntry Text="In Shared Code" /> 
    </StackLayout>
</ContentPage>

通过上述方法,我已成功禁用 iOS 和 Android 键盘上的听写按钮。

【讨论】:

    猜你喜欢
    • 2012-06-21
    • 1970-01-01
    • 2019-03-29
    • 2020-03-12
    • 1970-01-01
    • 2017-05-23
    • 2011-09-06
    • 1970-01-01
    相关资源
    最近更新 更多