加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x TextField的使用

发布时间:2020-12-14 16:53:46 所属栏目:百科 来源:网络整理
导读: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.t

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;
}
}

??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读