I have two methods that fire the Unity Event for that input field, I'm trying 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?
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":
inputField = gameObject.GetComponent<TMP_InputField>();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With