0

我正在尝试从多个字段(例如此页面)调用文本字段侦听器

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

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

login.lua 文件如下:

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

-- Forward declerations    
local userNameField

-- TextField Listener
local function fieldHandler( getObj )

     print( "This message is showing up :) " )

    -- Use Lua closure in order to access the TextField object
    return function( event )

        print( "This message is not showing up :( There is something wrong here!!!" )   

        if ( "began" == event.phase ) then
            -- This is the "keyboard has appeared" event
            getObj().text = ""
            getObj():setTextColor( 0, 0, 0, 255 )

        elseif ( "ended" == event.phase ) then
        -- This event is called when the user stops editing a field:
        -- for example, when they touch a different field or keyboard focus goes away

            print( "Text entered = " .. tostring( getObj().text ) ) -- display the text entered
        elseif ( "submitted" == event.phase ) then
        -- This event occurs when the user presses the "return" key
        -- (if available) on the onscreen keyboard
        -- Hide keyboard
           native.setKeyboardFocus( nil )
        end
    end -- "return function()" 
end

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

-- Called when the scene's view does not exist:
function scene:createScene( event )
    local group = self.view

-- Create our Text Field
    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 = "User Name"
    userNameField:setTextColor( 0, 0, 0, 12 )
end

请帮忙...

4

1 回答 1

1

我不知道 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-01T17:08:38.227 回答