// // 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, const char *delText, 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, const char *delText, int nLen) { CCLog("删除字符"); return false; }