游戏login的时候必需要求玩家输入用户名、密码,还要能够删除。json
cocostudio画一个textfield,直接读入好了;ui
textField类,继承读取的widget。lua
local textField = class("textField", function GUIReader:shareReader():widgetFromJsonFile(jsonPath) end)spa
--得到textField组件code
function testField:ctor()继承
self.mTextField_input = tolua.cast(Helper:seekWidgetByName(self, "textField_m"), "ccui.TextField")游戏
self:addCallback()事件
endip
--注册回调get
function testField:addCallback()
local keyListener = cc.EventListenerKeyBoard:create()
keyListener:registerScriptHandler(handler(self, self.onkeyPressed), cc.Handler.EVENT_KEYBOARD_PRESSED)
local currentScene = l_command.getCurrentScene()
currentScene:getEventDispatcher():addEventListenerWithSceneGraphPrioprity(keyListener, currentScene)
end
--删除事件,删除字母
function testField:onkeyPressed(keycode, event)
if keycode == cc.Keycode.KEYBACKSPACE then
local str = self.mTextField_input:getStringValue()
str = string.sub(str, 0, string.len(str) - 1)
self.mTextField_input:setText(str)
end
end