2013-09-01 38 views
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 

请帮助...

回答

1

不知电晕,但你的代码是有些奇怪。

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())