【问题标题】:how to hide on EditText soft keyboard windows 8 Metro Application?如何在 EditText 软键盘 windows 8 Metro Application 上隐藏?
【发布时间】:2012-05-23 06:12:59
【问题描述】:

我在使用 C# 的框架中有一个 EditText 和一个按钮。在编辑字段中写入并单击按钮后,我想隐藏虚拟软键盘。

【问题讨论】:

标签: c# xaml windows-8 windows-runtime winrt-xaml


【解决方案1】:

添加一个虚拟按钮并将焦点设置为它,键盘将被隐藏。

【讨论】:

  • 我这样做了,但设置了IsTabStop="true" 并将Height/Width 设置为"0",而不是Visibility="Collapsed"。这对我来说是神奇的设置组合。
【解决方案2】:

感谢您的提问。 我已经为这个问题找到了更好的解决方案。像这样

首先我们可以在 xaml 中添加处理程序

<Grid x:Name= Tapped="Grid_Tapped_1">
  ......
 </Grid >

然后我们像关注一样关注当前页面。效果很好。

private void Grid_Tapped_1(object sender, TappedRoutedEventArgs e)
        {
            this.Focus(FocusState.Programmatic);
        }

【讨论】:

    【解决方案3】:

    你不能。有关Input Hosting Manager and Soft Keyboard 行为的更多信息,您可以register to know when it shows or becomes hidden。但是,您无法以编程方式控制它是向上还是向下。

    【讨论】:

    • 我不明白为什么这是公认的答案。正如罗伯特在下面建议的那样,将焦点设置在隐藏按钮上效果很好。必须抓住一些极端情况,但最终的用户体验要好得多......
    【解决方案4】:

    当显示虚拟键盘的文本框将其属性 IsEnabled 设置为 false 时,虚拟键盘就会消失。之后我们可以立即将 is 设置为 true,虚拟键盘将保持隐藏状态。就像这样:

    MyTextBox.KeyDown += (s, a) => {
        if (a.Key == VirtualKey.Enter) {
            MyTextBox.IsEnabled = false;
            MyTextBox.IsEnabled = true;
        }
    };
    

    【讨论】:

      【解决方案5】:

      尝试设置Textbox`的IsReadOnly属性。

      我正在做一些“类似”的事情

          private void textbox_input_LostFocus(object sender, RoutedEventArgs e)
          {
              textbox_input.IsReadOnly = false;
          }
      
          private void textbox_input_Tapped(object sender, TappedRoutedEventArgs e)
          {
              if(e.PointerDeviceType != Windows.Devices.Input.PointerDeviceType.Mouse)
                  textbox_input.IsReadOnly = true;
              else
                  textbox_input.IsReadOnly = false;
          }
      

      截断后,如果用户不使用鼠标,我会按下键盘...

      当文本框为只读时,KeyDown 事件也会被触发,因此您可以直接使用数据来设置您的视图模型并更新您的文本框;)

      【讨论】:

        【解决方案6】:

        There 是一种解决方案,可以通过在单击按钮后自动设置容器的IsTabStop=true 为“提交”来隐藏触摸键盘。

        但是,顺便说一句,我注意到下次进入该页面时,EditText(应该是TextBox)将自动对焦,并显示触摸键盘。也许您最好在提交后禁用EditText。 (好像完成并阻塞了输入操作)

        【讨论】:

          【解决方案7】:

          我遇到了同样的问题,只是有一点不同。 当我从文本框切换到日期选择器时,软键盘不会消失。

          我尝试了您的所有建议,但没有任何效果。每次我的日期选择器出现奇怪的行为时,在我尝试了上述解决方案之一(或其他一些 stackoverflow 线程)之后。

          一段时间后,我通过 Google 找到了一些东西,效果非常好。 HERE

          在评论部分 Dusher16 写了一个非常干净的解决方案,它也适用于 WinRT / Win8 / Win8.1 / Metro 或你将如何称呼它。

          创建一个新类:

          using System.Runtime.InteropServices;
          using Windows.Devices.Input;
          
          namespace Your.Namespace
          {
              public static class TouchKeyboardHelper
              {
                  #region < Attributes >
          
                  private const int WmSyscommand = 0x0112; // Flag to received/send messages to the system.
                  private const int ScClose = 0xF060; // Param to indicate we want to close a system window.
          
                  #endregion < Attributes >
          
                  #region < Properties >
          
                  public static bool KeyboardAttached
                  {
                      get { return IsKeyboardAttached(); }
                  }
          
                  #endregion < Properties >
          
                  #region < Methods >
          
                  [DllImport("user32.dll")]
                  private static extern int FindWindow(string lpClassName, string lpWindowName); // To obtain an active system window handler.
          
                  [DllImport("user32.dll")]
                  private static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam); // To send a message to the system.
          
                  /// <summary>
                  /// To detect if a real keyboard is attached to the dispositive.
                  /// </summary>
                  /// <returns></returns>
                  private static bool IsKeyboardAttached()
                  {
                      KeyboardCapabilities keyboardCapabilities = new KeyboardCapabilities(); // To obtain the properties for the real keyboard attached.
                      return keyboardCapabilities.KeyboardPresent != 0 ? true : false;
                  }
          
                  /// <summary>
                  /// To close the soft keyboard
                  /// </summary>
                  public static void CloseOnscreenKeyboard()
                  {
                      // Retrieve the handler of the window 
                      int iHandle = FindWindow("IPTIP_Main_Window", ""); // To find the soft keyboard window.
                      if (iHandle > 0)
                      {
                          SendMessage(iHandle, WmSyscommand, ScClose, 0); // Send a close message to the soft keyboard window.
                      }
                  }
          
                  #endregion < Methods >
              }
          }
          

          例如,在某些 XAML.cs 文件中添加以下行:

          private void DatePicker_GotFocus(object sender, RoutedEventArgs e)
          {
              if (TouchKeyboardHelper.KeyboardAttached)
                  TouchKeyboardHelper.CloseOnscreenKeyboard();
          }
          

          【讨论】:

            猜你喜欢
            • 2012-02-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-12-13
            • 1970-01-01
            • 2013-12-06
            相关资源
            最近更新 更多