Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by xpajtimi · Jul 02, 2019 at 02:01 PM · keyboardinputfield

Pressing back button on Android to close/hide the keyboard is clearing my input field

I have two methods that fire the Unity Event for that input field, try to save the input field value into a variable and then give that value back to Input Field when back button is pressed, but it doesn't work on Android. It works just fine with Unity Editor

public string passwordHolder = "";

public void OnEditting()

    {
      if (Application.platform == RuntimePlatform.Android) 
       {
         if (!Input.GetKeyDown(KeyCode.Escape))
          {
             passwordHolder = passwordText.text;
          }
      }
  }


public void OnEndEdit()

{

     if (Application.platform == RuntimePlatform.Android) 
           {
                if (Input.GetKeyDown(KeyCode.Escape))
               {              
                      passwordText.text = passwordHolder;               
               }
          }
 }

What am I doing wrong?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CannJRB · Jul 15, 2019 at 10:10 PM 0
Share

I just want to clarify, you want the user to press the back button on their device after they have typed something into an input field?

If that is is the case, I don't think that will ever work on Android. I'm fairly certain that the OS will close any open keyboard and forget any typed input when the back button is pressed. I think that behavior would be expected by users.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by gillemp · Sep 17, 2019 at 08:10 PM

The "Input" does not work as normal when the keyboard is open. So you can not do the check you do with the "ESC" / back button.

What I have done is creating a method to listen to an event of the keyboard status changing. If it is going to hide (Canceled), I keep the previous text in the field.

I add the script to the object with the InputField component and this is the code that I have put in "Start":

         placeHolder = (TextMeshProUGUI) inputField.placeholder;
         inputField.onEndEdit.AddListener(EndEdit);
         inputField.onValueChanged.AddListener(Editing);
         inputField.onTouchScreenKeyboardStatusChanged.AddListener(ReportChangeStatus);


And this is the code of the methods:

     private void ReportChangeStatus(TouchScreenKeyboard.Status newStatus)
     {
         if (newStatus == TouchScreenKeyboard.Status.Canceled)
             keepOldTextInField = true;
     }
 
     private void Editing(string currentText)
     {
         oldEditText = editText;
         editText = currentText;
     }
   
     private void EndEdit(string currentText)
     {
         if (keepOldTextInField && !string.IsNullOrEmpty(oldEditText))
         {
             //IMPORTANT ORDER
             editText = oldEditText;
             inputField.text = oldEditText;
             
             keepOldTextInField = false;
         }
     }

It works for me and I hope it does for you aswell.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OscarAvilaDev · Sep 22, 2020 at 02:27 AM 1
Share

Dude, I know it's an old post, but thanks a lot! I mean, it only works if the stupid mobile input is active, but either way, Your code works perfectly. Thanks again

avatar image Yiming075 · Apr 27 at 01:34 PM 0
Share

Not working on UGUI input field.

avatar image
0

Answer by santi2493 · Mar 24 at 05:21 AM

I came looking for a similar problem. Don't know if this is what you where looking for but it works for me.

 public InputField inputfield;
 
 //Call method  in editor
 public void MethodCalled_OnEndEdit()
 {
    if (inputfield.wasCanceled)
    {
     //Do stuff
      return;
    }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

186 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I stop InputField focus/selection from activating device's native keyboard? 1 Answer

InputField keyboard hide suggestion 0 Answers

Keyboard not opening when I am trying to shift to next input field through script 0 Answers

Input keys not working ! please help me,Can't use input keys ! please help me ! 0 Answers

Handling Multiple U.I. Input Fields 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges