how to hide on EditText soft keyboard windows 8 Metro Application?

狂风中的少年 提交于 2019-12-30 07:39:29

问题


I'm having an EditText and a Button in my Frame using C#. After writing inside the edit field and clicking on the Button, I want to hide the virtual soft keyboard.


回答1:


You cannot. There is more information on the behavior of the Input Hosting Manager and Soft Keyboard and you can register to know when it shows or becomes hidden. But, you cannot programmatically control whether it's up or down.




回答2:


Add a dummy button and set focus to it and the keyboard will be hidden.




回答3:


Thanks for your question. i have get a better solution for this problem. like this

first we can add handler in xaml

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

then we focus current page like follow. it works well.

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



回答4:


When the textbox that showed the virtual keyboard has it’s propery IsEnabled set to false, the virtual keyboard disappears. We can immediately set is to true after that and the virtual keyboard will remain hidden. Just like this:

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



回答5:


Try to set the IsReadOnly property of the Textbox`.

I'm doing something "similar"

    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;
    }

With this snipped I suppress the keyboard if the user isn't using the mouse...

Also the KeyDown event is fired while the textbox is readonly so you could use the data directly to set your viewmodel and update over it your textbox ;)




回答6:


There is a solution which can hide the touch-keyboard by setting the container's IsTabStop=true automaticly after clicking your Button as "submit".

But, btw, I've noticed that the next time entering that page, the EditText (supposed to be a TextBox) will be auto-focused, and have the touch-keyboard showed. Maybe you'd better Disable the EditText after submitting. (seems to finish and block the input operation)




回答7:


I had the same problem, only with a little difference. When I switched from a textbox to a datepicker the softkeyboard won't disappear.

I tried all of your suggestions, but nothing worked like it should. Every time my datepicker had a strange behaviour, after I tried one of the above solutions (Or some of other stackoverflow threads).

After some time I found something via Google, which worked like a charm. HERE

In the comment section Dusher16 wrote a very clean solution, which works also for WinRT / Win8 / Win8.1 / Metro or how you will call it.

Create a new class:

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 >
    }
}

And in for example some XAML.cs file you add the following lines:

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


来源:https://stackoverflow.com/questions/10714431/how-to-hide-on-edittext-soft-keyboard-windows-8-metro-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!