键盘与文本框重叠

本文关键字:重叠 文本 键盘 | 更新日期: 2023-09-27 18:00:36

我正在WP8应用程序中为注册文档使用一个文本框列表。

文本框的数量相当大,因此用户必须在它们之间滚动。为了在一个字段和另一个字段之间导航,我添加了两个applicationbarIcons,next和previous。按下next将焦点更改为列表中的下一个文本框,并使用文本框的高度滚动滚动查看器的内容(在本例中为50)。

然而,有时,当将焦点切换到下面的元素时,键盘会覆盖文本框。(内容不会向上滚动)。

有没有办法强制文本框在键盘上方移动,即使它处于滚动视图中?

<ScrollViewer x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel>
            <TextBlock Text="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.STRING_CONTACT}" Margin="10,5" FontWeight="SemiBold" Foreground="#878780"></TextBlock>
            <StackPanel Margin="10,5" Height="190" Background="#F4F3F4">
                <TextBox LostFocus="firstNameTxt_LostFocus_1" GotFocus="firstNameTxt_GotFocus_1" Margin="0,-7" FontSize="23" x:Name="firstNameTxt"   BorderThickness="0" Background="Transparent" InputScope="PersonalFullName"><TextBox>
                <TextBox LostFocus="firstNameTxt_LostFocus_1" GotFocus="firstNameTxt_GotFocus_1" Margin="0,-7" FontSize="23" x:Name="lastNameTxt"    BorderThickness="0" Background="Transparent" InputScope="PersonalFullName"></my:DefaultTextBox>
                <TextBox LostFocus="firstNameTxt_LostFocus_1" GotFocus="firstNameTxt_GotFocus_1" Margin="0,-7" FontSize="23" x:Name="MobileTxt"  BorderThickness="0" InputScope="Number" Background="Transparent" ></TextBox>
                <TextBox LostFocus="firstNameTxt_LostFocus_1" GotFocus="firstNameTxt_GotFocus_1" Margin="0,-7" FontSize="23" x:Name="EmailTxt" BorderThickness="0" Background="Transparent">
        </StackPanel>
</ScrollViewer>

代码背后:

void left_Click(object sender, EventArgs e)
    {
        int index = this.controls.IndexOf(currentControl) - 1;
        if (index == -1)
        {
            this.Focus();
            return;
        }
        currentControl = this.controls[index];
        ContentPanel.ScrollToVerticalOffset(ContentPanel.VerticalOffset - 50);
        currentControl.Focus();

    }

键盘与文本框重叠

这是WP8上的一个常见问题。当一个文本框被聚焦时,它将翻译ApplicationRootVisual以使其进入视图。在某些情况下(当剪贴板打开时,或者在您的情况下),这不会很好地工作。解决方法是在TextBoxGotFocusLostFocus事件上将RootVisual手动转换为所需的垂直偏移。

private void TranslateRootVisualY(int yNew)
{      
  var rootFrame = Application.Current.RootVisual as PhoneApplicationFrame;
  rootFrame.RenderTransform = new CompositeTransform() {TranslateY = yNew};
}

在您的情况下,您可以取消自动翻译,并在GotFocus事件中使ScrollViewer滚动到所需偏移:

private void firstNameTxt_GotFocus_1(object sender, RoutedEventArgs e)
{
   TranslateRootVisualY(0);
   Dispatcher.BeginInvoke(() =>{
      double destOffset;
      //...calculate destination offset
      ContentPanel.ScrollToVerticalOffset(destOffset);
   });
}

destOffset可以从发送器和其他函数(如GetRectFromCharacterIndex )计算