Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Textfield clear on input for mobile

Discussion in 'UI Toolkit' started by wethings, Sep 9, 2021.

  1. wethings

    wethings

    Joined:
    Aug 26, 2018
    Posts:
    28
    Hi,

    trying to implement the thing where the textfield has a default value and will clear the value when the user wants to type something in. Below is the code snippet used

    Code (CSharp):
    1. void Start()
    2. {
    3.    myTextField.RegisterCallback<FocusInEvent>(ev => focusIn());
    4.    myTextField.RegisterCallback<FocusOutEvent>(ev => focusOut());
    5. }
    6.  
    7. void focusIn()
    8. {
    9.    if (myTextField.text.Equals(defaultTxt))
    10.       myTextField.value = "";
    11. }
    12.  
    13. void focusOut()
    14. {
    15.      if (string.IsNullOrEmpty(myTextField.value))
    16.           myTextField.value = defaultTxt;
    17. }
    18.  
    19.  
    Works fine in play mode, but when deployed to a mobile device like android I notice that when u press on the textfield it would call FocusInEvent and then FocusOutEvent immediately when the on screen keyboard pops up.

    Any other way to implement this?
     
  2. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    Try using
    Code (CSharp):
    1. myTextField.SetValueWithoutNotify("");
     
  3. wethings

    wethings

    Joined:
    Aug 26, 2018
    Posts:
    28
    Ah was not aware of that method.

    But doesn't help with the FocusOutEvent being called when the on screen keyboard appears.

    Tried using OnApplicationFocus but sometimes the textfield clears the input by itself... think theres something with the timing of things executing I'm not accounting for

    Maybe I'll just wait for them to implement placeholder text... lol 2022 eh?
     
    Last edited: Sep 10, 2021
  4. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    I think placeholer text (among most useful features for input fields) is scheduled for 2023 :/
    Don't remember where I read it, but will drop a link if I find it. Have not tested for mobile because all input is broken on Unity Remote, but I do have a solution for placeholder text that at least works for PC.
     
  5. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    Here is the link. It was 2022.1, not 2023.x.
     
  6. wethings

    wethings

    Joined:
    Aug 26, 2018
    Posts:
    28
    Yea I can get it to work on PC, just not mobile. Just tried iOS... it treats OnApplicationFocus differently from Android. Its a real mess, guess I really do have to wait.

    Ah! phew lol. Thanks a lot for your help!