Search Unity

[SOLVED] Can't save text from Android's keyboard input.

Discussion in 'Scripting' started by u_rs, Mar 22, 2018.

  1. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    There TouchScreenKeyboard opens after I press on a button. And I wan't keyboard's input be on button then.
    I can open keyboard on button click, enter text, but text isn't changing on button.


    Code (CSharp):
    1.  
    2. ...
    3. private string keyboardText;
    4. ....
    5.         _newButton.onClick.AddListener(ItemClicked);
    6.         _newButton.GetComponentInChildren<Text>().text = keyboardText;
    7. .....
    8.  
    9. void ItemClicked() {
    10.         keyboard = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.ASCIICapable, false, false, false, false);      
    11.         keyboardText = keyboard.text;
    12.     }
     
  2. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    429
    Have a look at this example: https://docs.unity3d.com/ScriptReference/TouchScreenKeyboard-done.html

    You need to update the text in an Update loop while the keyboard is active, so you'll see the changes. Or in the example on the documentation above, update the text when the keyboard is done editing

    Code (CSharp):
    1. void Update()
    2. {
    3.     if (keyboard != null && keyboard.active)
    4.         _newButton.GetComponentInChildren<Text>().text = keyboard.text;
    5. }
    Or run a Coroutine when you click your button

    Code (CSharp):
    1.  
    2. IEnumerator EditTextWithKeyboard(TouchScreenKeyboard keyboard, Text t)
    3. {
    4.     while (!keyboard.done)
    5.     {
    6.         t.text = keyboard.text;
    7.         yield return null;
    8.     }
    9. }
    10.  
     
    PokerDawg and u_rs like this.
  3. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    Thank you very much, it works now.;)