cocos2dx lua 怎样实现cceditbox

2025-04-30 04:20:44
推荐回答(1个)
回答1:

--定义编辑框CCEditBox的回调事件
local function editBoxTextEventHandle(strEventName,pSender)
local edit = tolua.cast(pSender,"CCEditBox")
local strFmt
if strEventName == "began" then --编辑框开始编辑时调用
strFmt = string.format("editBox %p DidBegin !", edit)
print(strFmt)
elseif strEventName == "ended" then --编辑框完成时调用
strFmt = string.format("editBox %p DidEnd !", edit)
print(strFmt)
elseif strEventName == "return" then --编辑框return时调用
strFmt = string.format("editBox %p was returned !",edit)
--判断是哪个编辑框,在多个编辑框同时绑定此函数时 需判断时哪个编辑框
if edit == EditName then
--当编辑框EditName 按下return 时到此处
elseif edit == EditPassword then
--当编辑框EditPassword 按下return 时到此处
elseif edit == EditEmail then
--当编辑框EditEmail 按下return 时到此处
end
print(strFmt)
elseif strEventName == "changed" then --编辑框内容改变时调用
strFmt = string.format("editBox %p TextChanged, text: %s ", edit, edit:getText())
print(strFmt)
end
end
--定义一个编辑框CCEditBox
local editBoxSize = CCSizeMake(120, 20) --设置编辑框尺寸
EditName = CCEditBox:create(editBoxSize, CCScale9Sprite:create("green_edit.png"))
--CCEditBox编辑框绑定事件editBoxTextEventHandle
EditName:registerScriptEditBoxHandler(editBoxTextEventHandle)
--省略其他编辑框的定义