首页 > 解决方案 > 如何在 xamarin 表单中自动大写键盘/输入

问题描述

我有两个条目用户名和密码。用户名和密码始终为大写。我如何强制键盘或输入内容时输入的文本为大写?

<Frame StyleClass="lpframe" x:Name="usernameFrame" CornerRadius="5" BorderColor="Transparent" HasShadow="False">
                <StackLayout>
                    <Label Text="USERNAME" StyleClass="lbl-login">
                        <Label.FontFamily>
                            <OnPlatform x:TypeArguments="x:String">
                                <On Platform="Android" Value="OpenSans-Semibold.ttf#OpenSans-Semibold"/>
                            </OnPlatform>
                        </Label.FontFamily>
                    </Label>
                    <local:CustomEntry Placeholder="Username" PlaceholderColor="#879baa" Unfocused="entUser_Unfocused" StyleClass="form-control" ReturnType="Next" x:Name="entUser">
                        <local:CustomEntry.FontFamily>
                            <OnPlatform x:TypeArguments="x:String">
                                <On Platform="Android" Value="OpenSans-Regular.ttf#OpenSans-Regular"/>
                            </OnPlatform>
                        </local:CustomEntry.FontFamily>
                    </local:CustomEntry>
                </StackLayout>
            </Frame>
            <Frame StyleClass="lpframe" x:Name="passwordFrame" CornerRadius="5" BorderColor="Transparent" HasShadow="False">
                <StackLayout>
                    <Label Text="PASSWORD" StyleClass="lbl-login">
                        <Label.FontFamily>
                            <OnPlatform x:TypeArguments="x:String">
                                <On Platform="Android" Value="OpenSans-Semibold.ttf#OpenSans-Semibold"/>
                            </OnPlatform>
                        </Label.FontFamily>
                    </Label>
                    <local:CustomEntry Placeholder="Password" PlaceholderColor="#879baa" Unfocused="entPassword_Unfocused" IsPassword="True" StyleClass="form-control" ReturnType="Go" x:Name="entPassword">
                        <local:CustomEntry.FontFamily>
                            <OnPlatform x:TypeArguments="x:String">
                                <On Platform="Android" Value="OpenSans-Regular.ttf#OpenSans-Regular"/>
                            </OnPlatform>
                        </local:CustomEntry.FontFamily>
                    </local:CustomEntry>
                </StackLayout>
            </Frame>

标签: xamlxamarinxamarin.formsxamarin.forms.entry

解决方案


现在您可以使用 Create 方法工厂来修改键盘行为,如下所示:

<Entry Placeholder="Enter text here">
<Entry.Keyboard>
    <Keyboard x:FactoryMethod="Create">
        <x:Arguments>
            <KeyboardFlags>CapitalizeWord</KeyboardFlags>
        </x:Arguments>
    </Keyboard>
</Entry.Keyboard>

这是官方文档:自定义键盘

以下是所有支持的标志:

  • 无 - 键盘不添加任何功能。
  • CapitalizeSentence - 表示每个输入的句子的第一个单词的第一个字母将自动大写。
  • 拼写检查 - 表示将对输入的文本执行拼写检查。
  • 建议 - 表示将对输入的文本提供单词完成。
  • CapitalizeWord – 表示每个单词的第一个字母将自动大写。
  • CapitalizeCharacter – 表示每个字符都将自动大写。
  • CapitalizeNone – 表示不会发生自动大写。
  • 全部 - 表示将对输入的文本进行拼写检查、单词补全和句子大写。

推荐阅读