使用Lua在Corona SDK上检测键盘是否可见

由于我的两个文本框位于屏幕底部,我开发了以下代码,在触摸任一文本框时将它们移到一个新位置(以避免键盘重叠):

local textField = native.newTextField( display.contentCenterX, display.contentCenterY + 60, 200, 40 )
textField.placeholder = "Email"
textField.isEditable = true

local textField2 = native.newTextField( display.contentCenterX, display.contentCenterY + 110, 200, 40 )
textField2.placeholder = "Password"
textField2.isEditable = true

 --function to handle events

 local function touchListener(oEvent)
    local oTextField = oEvent.target

    if "began" == oEvent.phase then

      textField.y = display.contentCenterY - 120
      textField2.y = display.contentCenterY - 70
      myText.y = 55

      local button2 = widget.newButton
{
    x = display.contentCenterX,
    y = display.contentCenterY - 20,
    id = "loginbutton",
    defaultFile = "buttonDefault.png",
    font = "Georgia",
    labelColor = { default={ 1, 1, 1 } },
    label = "Login"

}

    elseif "editing" == oEvent.phase then

    elseif "submitted" == oEvent.phase then

      textField.y = display.contentCenterY - 120
      textField2.y = display.contentCenterY - 70
      myText.y = 55

      button2.isVisible = false
      native.setKeyboardFocus( nil ) --hides keyboard

    elseif "ended" == oEvent.phase then

        textField.y = display.contentCenterY + 60
        textField2.y = display.contentCenterY + 110
        myText.y = display.contentCenterY - 3

    end
end

textField:addEventListener( "userInput", touchListener )

是否有人能帮助我了解如何使用 Lua 检测键盘不可见,以便如果用户退出文本框(通过按“back”功能或按放开外部)我可以将文本框返回其原始位置?

预先感谢您的帮助!

点赞
用户869951
用户869951

正确的做法是在需要时创建所有需要的小部件并仅在需要时显示/隐藏它们。例如,

  1. 在场景初始化中(例如createScene事件处理程序)创建 textfieldtextfield2button2,并在两个文本字段上设置一个点击监听器。在按钮上设置一个点击监听器。
  2. 在点击监听器中,移动两个文本字段并使按钮可见。
  3. 在按钮点击事件处理程序中,恢复两个文本字段的y位置并隐藏按钮。
2014-08-04 00:51:33