我正在尝试从多个字段调用文本字段侦听器,如此页面
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
请帮助......
答案 0 :(得分: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() )