2012-05-23 172 views
1

我在使用C#的框架中有一個EditText和一個按鈕。在編輯區內寫入並單擊按鈕後,我想要隱藏虛擬軟鍵盤。如何隱藏EditText軟鍵盤windows 8 Metro應用程序?

+0

這是不可能的Win8 CP。可能的重複:http://stackoverflow.com/questions/10129550/show-hide-keyboard-programmatically-on-windows8 –

+0

如果在這個問題上的任何其他選擇。 – Narasimha

回答

2

你不能。有關於Input Hosting Manager and Soft Keyboard的行爲的更多信息,您可以register to know when it shows or becomes hidden。但是,您無法以編程方式控制它是啓動還是關閉。

+1

我不明白爲什麼這是被接受的答案。按照羅伯特的建議,將焦點放在隱藏的按鈕上,完美地工作。一個人必須抓住一些角落的情況,但最終的用戶體驗要好得多...... –

0

嘗試設置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事件而解僱文本框是隻讀的,因此你可以直接利用這些數據來設置您的視圖模型,並更新了它的文本框;)

7

添加一個虛擬按鈕,將焦點設置到它和鍵盤將被隱藏。

+0

我這樣做,但設置了「IsTabStop =」true「'並將'Height' /'Width'設置爲'」0「',而不是'可見性=「摺疊」'。這是我對環境的神奇結合。 –

3

感謝您的問題。 我已經爲這個問題得到了更好的解決方案。這樣

首先我們可以在XAML

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

添加處理程序,然後我們注重當前頁面類似如下。它運作良好。

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

當顯示虛擬鍵盤的文本框將它的IsEnabled設置爲false時,虛擬鍵盤消失。我們可以立即設置爲true,虛擬鍵盤將保持隱藏狀態。就像這樣:

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

There是可以通過設置隱藏觸摸鍵盤溶液的容器的IsTabStop=true點擊您的按鈕爲「提交」後,全自動。

但是,順便說一句,我已經注意到,下一次進入該頁面時,EditText(應該是TextBox)將自動對焦,並有觸摸鍵盤顯示。也許你最好在提交後禁用EditText。 (似乎完成並阻止輸入操作)

0

我有同樣的問題,只是有一點差別。 當我從文本框切換到日期選擇器時,軟鍵盤不會消失。

我嘗試了所有的你的建議,但是毫無效果像它應該。每次我的datepicker有一個奇怪的行爲後,我嘗試了上述解決方案之一(或其他一些其他stackoverflow線程)。

一段時間後,我發現通過谷歌的東西,它的工作就像一個魅力。 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(); 
}