5. coccos2d-x CCTextFieldTTF 输入框

来源:互联网 发布:阿里云华东2怎么样 编辑:程序博客网 时间:2024/05/20 13:13
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"using namespace cocos2d;class HelloWorld : public cocos2d::CCLayer ,public cocos2d::CCTextFieldDelegate{public:    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)    virtual bool init();    // there's no 'id' in cpp, so we recommend to return the class instance pointer    static cocos2d::CCScene* scene();        // a selector callback    void menuCloseCallback(CCObject* pSender);        //用户启动虚拟键盘时回调函数    virtual bool onTextFieldAttachWithIME(CCTextFieldTTF * sender);    //当用户关闭虚拟键盘时回调    virtual bool onTextFieldDetachWithIME(CCTextFieldTTF * sender);    //当用户进行输入时回调函数    virtual bool onTextFieldInsertText(CCTextFieldTTF * sender, const char * text, int nLen);    //当用户删除文字时回调函数    virtual bool onTextFieldDeleteBackward(CCTextFieldTTF * sender, const char * delText, int nLen);    // preprocessor macro for "static create()" constructor ( node() deprecated )    CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"using namespace cocos2d;using namespace CocosDenshion;CCScene* HelloWorld::scene(){    // 'scene' is an autorelease object    CCScene *scene = CCScene::create();        // 'layer' is an autorelease object    HelloWorld *layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !CCLayer::init() )    {        return false;    }    /////////////////////////////    // 2. add a menu item with "X" image, which is clicked to quit the program    //    you may modify it.    // add a "close" icon to exit the progress. it's an autorelease object    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(                                        "CloseNormal.png",                                        "CloseSelected.png",                                        this,                                        menu_selector(HelloWorld::menuCloseCallback) );    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );    // create menu, it's an autorelease object    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);    pMenu->setPosition( CCPointZero );    this->addChild(pMenu, 1);        //输入框 CCTextFieldTTF        CCTextFieldTTF *textFileds=CCTextFieldTTF::textFieldWithPlaceHolder("请输入用户名", "Helvetica", 20);    textFileds->setPosition(ccp(100, 300));    this->addChild(textFileds);    //绑定接口    textFileds->setDelegate(this);    //开启输入    textFileds->attachWithIME();    //关闭输入//    textFileds->detachWithIME();             return true;}void HelloWorld::menuCloseCallback(CCObject* pSender){    CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif}//用户启动虚拟键盘时回调函数 bool HelloWorld:: onTextFieldAttachWithIME(CCTextFieldTTF * sender){    CCLOG("启动输入");    return false;//    return true;//不启动}//当用户关闭虚拟键盘时回调 bool HelloWorld:: onTextFieldDetachWithIME(CCTextFieldTTF * sender){    CCLOG("关闭输入");    return false;}//当用户进行输入时回调函数 bool HelloWorld:: onTextFieldInsertText(CCTextFieldTTF * sender, const char * text, int nLen){    CCLOG("输入字符...");    return false;    }//当用户删除文字时回调函数 bool HelloWorld:: onTextFieldDeleteBackward(CCTextFieldTTF * sender, const char * delText, int nLen){    CCLOG("删除字符...");    return false;}



0 0
原创粉丝点击