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?
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.
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.
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
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;
}
}
Your answer
Follow this Question
Related Questions
How to disable native input field from Android TouchScreenKeyboard. 0 Answers
Is there a way for inputfields to follow keyboard when i select them ? 0 Answers
Remap keyboard/keys for InputField 0 Answers
Input Field not getting selected? 0 Answers
How can I stop InputField focus/selection from activating device's native keyboard? 1 Answer