Cocos2d-x TextField的使用

void textFieldEvent(Ref *pSender,cocos2d::ui::TextField::EventType type);

cocos2d::ui::Text* _displayValueLabel;


Size size = Director::getInstance()->getVisibleSize();
_displayValueLabel = cocos2d::ui::Text::create("No Event","Marker Felt.ttf",32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f,-1.0f));
_displayValueLabel->setPosition(Vec2(size.width / 2.0f,size.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
this->addChild(_displayValueLabel);


cocos2d::ui::TextField* textField = cocos2d::ui::TextField::create("input words here","Arial",30);
textField->setMaxLengthEnabled(true);
textField->setMaxLength(3);
textField->setPosition(Vec2(size.width / 2.0f,size.height / 2.0f));
textField->addEventListener(CC_CALLBACK_2(HelloWorld::textFieldEvent,this));
this->addChild(textField);


void HelloWorld::textFieldEvent(Ref *pSender,cocos2d::ui::TextField::EventType type)
{
switch (type)
{
case cocos2d::ui::TextField::EventType::ATTACH_WITH_IME:
{
cocos2d::ui::TextField* textField = dynamic_cast<cocos2d::ui::TextField*>(pSender);
Size screenSize = CCDirector::getInstance()->getWinSize();
textField->runAction(CCMoveTo::create(0.225f,
Vec2(screenSize.width / 2.0f,screenSize.height / 2.0f + textField->getContentSize().height / 2.0f)));
_displayValueLabel->setString(String::createWithFormat("attach with IME max length %d",textField->getMaxLength())->getCString());
}
break;

case cocos2d::ui::TextField::EventType::DETACH_WITH_IME:
{
cocos2d::ui::TextField* textField = dynamic_cast<cocos2d::ui::TextField*>(pSender);
Size screenSize = CCDirector::getInstance()->getWinSize();
textField->runAction(CCMoveTo::create(0.175f,Vec2(screenSize.width / 2.0f,screenSize.height / 2.0f)));
_displayValueLabel->setString(String::createWithFormat("detach with IME max length %d",textField->getMaxLength())->getCString());
}
break;

case cocos2d::ui::TextField::EventType::INSERT_TEXT:
{
TextField* textField = dynamic_cast<cocos2d::ui::TextField*>(pSender);
_displayValueLabel->setString(String::createWithFormat("insert words max length %d",textField->getMaxLength())->getCString());
}
break;

case cocos2d::ui::TextField::EventType::DELETE_BACKWARD:
{
TextField* textField = dynamic_cast<cocos2d::ui::TextField*>(pSender);
_displayValueLabel->setString(String::createWithFormat("delete word max length %d",textField->getMaxLength())->getCString());
}
break;

default:
break;
}
}



相关文章

操作步骤 1、创建cocos2d-x工程 2、新建 Scene1.cpp Scene1.h Scene1.h代码 #ifndef __SCENE1_H__#defi...
开发环境:OS(WINDOWS 8.1 X64 企业版) cocos2d-x 2.2.1 vs2010 想给vs安装上cocos的模版,执行Install...
把创建项目做成一个批处理,当创建项目时可以省时省力很多。 操作步骤 1、在 E:cocos2d-x-2.2.1toolspr...
https://www.cnblogs.com/JiaoQing/p/3906780.html 四个响应函数 1 EventListenerPhysicsContact* evC...
转载于 http://www.cnblogs.com/kenkofox/p/3926797.html 熟悉js的dom事件或者flash事件的,基本都能立...
ScrollView(滚动容器)加载大量item时会导致游戏界面的卡顿,严重时整个界面会出现卡死的情况。最近项...