2013-09-01 37 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())