简体   繁体   English

在Android上按返回按钮以关闭/隐藏键盘正在清除我的输入字段Unity

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

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. 我有两种方法可以触发该输入字段的Unity事件,尝试将输入字段的值保存到变量中,然后在按下后退按钮时将该值返回给输入字段,但是在Android上不起作用。 It works just fine with Unity Editor 它与Unity Editor一起正常工作

public string passwordHolder = ""; 公共字符串passwordHolder =“”;

public void OnEditting() 公共无效OnEditting()

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

} }

public void OnEndEdit() 公共无效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. 因此,您无法使用“ ESC” /返回按钮进行检查。

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组件将脚本添加到对象中,这是我在“开始”中输入的代码:

    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. 它对我有用,我希望对您也有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM