游戲login的時候必須要求玩家輸入用戶名、密碼,還要可以刪除。
cocostudio畫一個textfield,直接讀入好了;
textField類,繼承讀取的widget。
local textField = class("textField", function GUIReader:shareReader():widgetFromJsonFile(jsonPath) end)
--獲得textField組件
function testField:ctor()
self.mTextField_input = tolua.cast(Helper:seekWidgetByName(self, "textField_m"), "ccui.TextField")
self:addCallback()
end
--注冊回調
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