监听多个文本字段的侦听器

我正在尝试从多个字段调用文本字段侦听器,就像这个页面一样:

http://docs.coronalabs.com/api/library/native/newTextField.html#listener-optional

当用户开始在输入字段中输入时,处理程序函数正常调用,但位于处理程序中的闭包未被调用。

以下是login.lua文件:

local storyboard = require("storyboard")
local scene = storyboard.newScene()

--前置声明
local userNameField

--文本字段侦听器
local function fieldHandler(getObj)

     print("这条消息正在显示:)")

     --使用Lua闭包以便访问TextField对象
     return function(event)

        print("这条消息没有显示:(这里有些问题!!!)")

        if ("began" == event.phase) then
            --这是“键盘已经出现”事件
            getObj().text = ""
            getObj():setTextColor(0, 0, 0, 255)

        elseif ("ended" == event.phase) then
        --当用户停止编辑字段时调用此事件:
        --例如,当他们触摸不同的字段或键盘焦点消失时

            print("输入的文本="..tostring(getObj().text)) --显示输入的文本
        elseif ("submitted" == event.phase) then
        --当用户按下“返回”键时触发此事件
        --(如果可用)在屏幕键盘上
        --隐藏键盘
           native.setKeyboardFocus(nil)
        end
    end --“return function()”
end

local function userNameFieldHandler(event)
   local myfunc =  fieldHandler(function() return userNameField end) --传递文本字段对象
end

--当场景的视图不存在时调用:
function scene:createScene(event)
    local group = self.view

--创建我们的文本字段
userNameField = native.newTextField(display.contentWidth * 0.1, display.contentHeight * 0.5, display.contentWidth * 0.8, display.contentHeight * 0.08)

    userNameField:addEventListener("userInput", userNameFieldHandler)
    userNameField.font = native.newFont(native.systemFontBold, 22)
    userNameField.text = "用户名"
    userNameField:setTextColor(0, 0, 0, 12)
end

请帮忙...

点赞
用户2633423
用户2633423

我不知道 Corona,但你的代码有点奇怪。

userNameFieldHandler 并没有做太多的事情,它只是创建了一个调用 fieldHandler 并将其存储在一个从未被使用的本地变量 myfunc 中的处理程序。你确定你的意思不是这个吗:

local function userNameFieldHandler( event )
   local myfunc =  fieldHandler(
      function() return userNameField end ) -- passes the text field object
   return myfunc --<<<<--- added return
end

而当你添加事件侦听器时,你可能是指这样的(注意添加的 ()):

userNameField:addEventListener( "userInput", userNameFieldHandler() )
2013-09-01 17:08:38