Cocos2dx使用TextField實現輸入框


游戲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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM