Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin 输入文本不对齐(-和数字)_Xamarin_Xamarin.forms - Fatal编程技术网

Xamarin 输入文本不对齐(-和数字)

Xamarin 输入文本不对齐(-和数字),xamarin,xamarin.forms,Xamarin,Xamarin.forms,我试图建立电子邮件代码验证程序。我正在向客户发送6位数字,以便在输入时输入他的代码。 但是-和数字有不同的宽度,这导致当用户输入数字时,我将-替换为数字,但它移动不同的对齐方式。我需要将它们设置为同一行。我怎么做 第一个是空框,第二个是数字和空格,第三个是应该是什么样子。查看您的代码会很有帮助,但我要尝试一下。使用FormattedText,并将每个项目或数字作为跨度 文本颜色为透明的条目上的图层。当用户输入值时,只需将Item1-Item6设置为根据需要显示。这是使用Xamarin.Forms

我试图建立电子邮件代码验证程序。我正在向客户发送6位数字,以便在输入时输入他的代码。 但是-和数字有不同的宽度,这导致当用户输入数字时,我将-替换为数字,但它移动不同的对齐方式。我需要将它们设置为同一行。我怎么做


第一个是空框,第二个是数字和空格,第三个是应该是什么样子。

查看您的代码会很有帮助,但我要尝试一下。使用FormattedText,并将每个项目或数字作为跨度


文本颜色为透明的条目上的图层。当用户输入值时,只需将Item1-Item6设置为根据需要显示。

这是使用Xamarin.Forms的完美示例

如果您查看该部分,您将看到如果您决定采用比例尺寸,将会发生什么:

比例–按剩余空间的比例调整列和行的大小。在C和XAML中指定为值和GridUnitType.Star,并作为所需的值。用*指定一行/列将使其填满可用空间

这意味着,如果希望有6个相等的列,可以这样做:

这样,每列将与其余列相等,并且正好是网格所占大小的1/6

您可以简单地将控件放置在网格中,并相应地对齐它们,就可以开始了


另外,如果你有一些特殊的符号,或者数字和字母的混合体,它们可能仍然没有完全对齐,你必须使用一种字体,我将HorizontalTextAlignment设置为居中

<Grid HorizontalOptions="CenterAndExpand" ColumnSpacing="0" WidthRequest="205">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Entry Placeholder="—" HorizontalOptions="Center" HorizontalTextAlignment="Center" Grid.Column="0"
                             Keyboard="Numeric" MaxLength="1" TextChanged="Entry_TextChanged"></Entry>
                        <Entry Placeholder="—" HorizontalOptions="Center" HorizontalTextAlignment="Center" Grid.Column="1"
                             Keyboard="Numeric" MaxLength="1" TextChanged="Entry_TextChanged"></Entry>
                        <Entry Placeholder="—" HorizontalOptions="Center" HorizontalTextAlignment="Center" Grid.Column="2"
                             Keyboard="Numeric" MaxLength="1" TextChanged="Entry_TextChanged"></Entry>
                        <Entry Placeholder="—" HorizontalOptions="Center" HorizontalTextAlignment="Center" Grid.Column="3"
                             Keyboard="Numeric" MaxLength="1" TextChanged="Entry_TextChanged"></Entry>
                        <Entry Placeholder="—" HorizontalOptions="Center" HorizontalTextAlignment="Center" Grid.Column="4"
                             Keyboard="Numeric" MaxLength="1" TextChanged="Entry_TextChanged"></Entry>
                        <Entry Placeholder="—" HorizontalOptions="Center" HorizontalTextAlignment="Center" Grid.Column="5"
                             Keyboard="Numeric" MaxLength="1"></Entry>
                    </Grid>
并添加此C代码,以便在输入值时关注下一个条目

    private void Entry_TextChanged(object sender, TextChangedEventArgs e)
    {
        var entry = sender as Entry;
        var list = (entry.Parent as Grid).Children;
        var index = list.IndexOf(entry);
        int nextIndex = 0;

        if (e.NewTextValue.Length > 0)
            nextIndex = (index + 1) >= list.Count ? 0 : index + 1;
        else if (e.NewTextValue.Length < 1 && index > 0)
            nextIndex = (index - 1);

        var next = list.ElementAt(nextIndex);
        next?.Focus();
    }

使用固定宽度font@Jason谢谢,这可能是答案。谢谢,但我需要做一个条目。网格中没有多个。顺便说一句,网格是mu最喜欢的布局。给我们展示更多的代码,这样我们就可以看到您当前的实现。同时,@ottermatic有一个关于FormattedTextThank的很好的建议,我会尝试。我将条目设置在带有标签的同一网格行上,但当我单击条目时,光标会闪烁。我只需要所有透明的输入字段。您可以为该项执行自定义渲染器,以将光标设置为透明