前端之家收集整理的这篇文章主要介绍了
Cocos2d-x_CCTextFieldTTF输入框,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//
// HelloWorldScene.h
//
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
class HelloWorld : public cocos2d::CCLayer,public cocos2d::CCTextFieldDelegate
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
void menuCloseCallback(CCObject* pSender);
CREATE_FUNC(HelloWorld);
virtual bool onTextFieldAttachWithIME(CCTextFieldTTF *pSender);
virtual bool onTextFieldDetachWithIME(CCTextFieldTTF *pSender);
virtual bool onTextFieldInsertText(CCTextFieldTTF *pSender,const char *delText,int nLen);
virtual bool onTextFieldDeleteBackward(CCTextFieldTTF *pSender,int nLen);
};
#endif
<pre name="code" class="cpp">//
// HelloWorldScene.h
//
#include "HelloWorldScene.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCTextFieldTTF *textField = CCTextFieldTTF::textFieldWithPlaceHolder("点击输入...","Helvetica",24);
textField->setPosition(ccp(visibleSize.width*0.5,visibleSize.height*0.7));
textField->setDelegate(this);
textField->attachWithIME();
this->addChild(textField);
return true;
}
// CCTextFieldTTFDelegate
bool HelloWorld::onTextFieldAttachWithIME(CCTextFieldTTF *pSender)
{
CCLog("启动输入");
return false;
}
bool HelloWorld::onTextFieldDetachWithIME(CCTextFieldTTF *pSender)
{
CCLog("关闭输入");
return false;
}
bool HelloWorld::onTextFieldInsertText(CCTextFieldTTF *pSender,const char *text,int nLen)
{
CCLog("输入字符");
return false;
}
bool HelloWorld::onTextFieldDeleteBackward(CCTextFieldTTF *pSender,int nLen)
{
CCLog("删除字符");
return false;
}
原文链接:https://www.f2er.com/cocos2dx/346834.html