cocos2dx cceditbox使用注意事项

基本用法见testcpp .cocos2d-x版本为2.1.4

.h文件

 

注意:这里继承于CCEditBoxDelegate代理,不直接继承于CCEditBox,而是将CCEditBox作为组合使用。

并将本身的Layer作为被代理对象,重载CCEditBoxDelegate接口。

CCEditBox的默认字体色是白色若是用白色的底框则看不到输入内容,所有务必检查字体颜色

CCeditBox在安卓下的坐标与cocos2dx其他控件一致,锚点也生效,win下的效果与安卓上效果一致。

 

class EditBoxTest : public cocos2d::CCLayer, public cocos2d::extension::CCEditBoxDelegate
{
public:
    EditBoxTest();
    virtual ~EditBoxTest();
    void toExtensionsMainLayer(cocos2d::CCObject *sender);
   
    virtual void editBoxEditingDidBegin(cocos2d::extension::CCEditBox* editBox);
    virtual void editBoxEditingDidEnd(cocos2d::extension::CCEditBox* editBox);
    virtual void editBoxTextChanged(cocos2d::extension::CCEditBox* editBox, const std::string& text);
    virtual void editBoxReturn(cocos2d::extension::CCEditBox* editBox);
private:
    cocos2d::CCLabelTTF* m_pTTFShowEditReturn;
    cocos2d::extension::CCEditBox* m_pEditName;
    cocos2d::extension::CCEditBox* m_pEditPassword;
    cocos2d::extension::CCEditBox* m_pEditEmail;
};

 

.cpp文件

USING_NS_CC;
USING_NS_CC_EXT;


EditBoxTest::EditBoxTest()
{
    CCPoint visibleOrigin = CCEGLView::sharedOpenGLView()->getVisibleOrigin();
    CCSize visibleSize = CCEGLView::sharedOpenGLView()->getVisibleSize();
   
    CCSprite* pBg = CCSprite::create("Images/HelloWorld.png");
    pBg->setPosition(ccp(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/2));
    addChild(pBg);
   
    m_pTTFShowEditReturn = CCLabelTTF::create("No edit control return!", "", 30);
    m_pTTFShowEditReturn->setPosition(ccp(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y + visibleSize.height - 50));
    addChild(m_pTTFShowEditReturn);
   
    // Back Menu
    CCMenuItemFont *itemBack = CCMenuItemFont::create("Back", this, menu_selector(EditBoxTest::toExtensionsMainLayer));
    itemBack->setPosition(ccp(visibleOrigin.x+visibleSize.width - 50, visibleOrigin.y+25));
    CCMenu *menuBack = CCMenu::create(itemBack, NULL);
    menuBack->setPosition(CCPointZero);
    addChild(menuBack);
   
    CCSize editBoxSize = CCSizeMake(visibleSize.width - 100, 60);

    // top 创建一个CCEditBox控件。这个控件在win下和安卓下并没有位置对齐的问题了。它的输入框与输入背景是一致的
    m_pEditName = CCEditBox::create(editBoxSize, CCScale9Sprite::create("extensions/green_edit.png"));
    m_pEditName->setPosition(ccp(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height*3/4));
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    m_pEditName->setFontName("Paint Boy");
#else
 m_pEditName->setFontName("fonts/Paint Boy.ttf");
#endif
    m_pEditName->setFontSize(25);              // 设置输入后字体的大小
    m_pEditName->setFontColor(ccRED);     // 注意这里有坑,CCEditBox的默认字体色是白色,若是用白色的底框则看不到输入内容
    m_pEditName->setPlaceHolder("Name:");   // 设置占位符的字样
    m_pEditName->setPlaceholderFontColor(ccWHITE);    // 设置占位符的颜色
    m_pEditName->setMaxLength(8);                                                     // 设置最大可输入长度
    m_pEditName->setReturnType(kKeyboardReturnTypeDone);
    m_pEditName->setDelegate(this);
    addChild(m_pEditName);
   
    // middle
    m_pEditPassword = CCEditBox::create(editBoxSize, CCScale9Sprite::create("extensions/orange_edit.png"));
    m_pEditPassword->setPosition(ccp(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/2));
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
 m_pEditPassword->setFont("American Typewriter", 30);
#else
 m_pEditPassword->setFont("fonts/American Typewriter.ttf", 30);
#endif
    m_pEditPassword->setFontColor(ccGREEN);
    m_pEditPassword->setPlaceHolder("Password:");
    m_pEditPassword->setMaxLength(6);
    m_pEditPassword->setInputFlag(kEditBoxInputFlagPassword);
    m_pEditPassword->setInputMode(kEditBoxInputModeSingleLine);
    m_pEditPassword->setDelegate(this);
    addChild(m_pEditPassword);
   
    // bottom
    m_pEditEmail = CCEditBox::create(CCSizeMake(editBoxSize.width, editBoxSize.height), CCScale9Sprite::create("extensions/yellow_edit.png"));
    m_pEditEmail->setPosition(ccp(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/4));
    m_pEditEmail->setAnchorPoint(ccp(0.5, 1.0f));
    m_pEditEmail->setPlaceHolder("Email:");
    m_pEditEmail->setInputMode(kEditBoxInputModeEmailAddr);
    m_pEditEmail->setDelegate(this);
    addChild(m_pEditEmail);
   
    this->setPosition(ccp(10, 20));
}

EditBoxTest::~EditBoxTest()
{
   
}

void EditBoxTest::toExtensionsMainLayer(cocos2d::CCObject *sender)
{
    ExtensionsTestScene *pScene = new ExtensionsTestScene();
    pScene->runThisTest();
    pScene->release();
}

void EditBoxTest::editBoxEditingDidBegin(cocos2d::extension::CCEditBox* editBox)
{
    CCLog("editBox %p DidBegin !", editBox);
}

void EditBoxTest::editBoxEditingDidEnd(cocos2d::extension::CCEditBox* editBox)
{
    CCLog("editBox %p DidEnd !", editBox);
}

void EditBoxTest::editBoxTextChanged(cocos2d::extension::CCEditBox* editBox, const std::string& text)
{
    CCLog("editBox %p TextChanged, text: %s ", editBox, text.c_str());
}

void EditBoxTest::editBoxReturn(CCEditBox* editBox)
{
    CCLog("editBox %p was returned !",editBox);
   
    if (m_pEditName == editBox)
    {
        m_pTTFShowEditReturn->setString("Name EditBox return !");
    }
    else if (m_pEditPassword == editBox)
    {
        m_pTTFShowEditReturn->setString("Password EditBox return !");
    }
    else if (m_pEditEmail == editBox)
    {
        m_pTTFShowEditReturn->setString("Email EditBox return !");
    }
}

 

你可能感兴趣的:(cocos2dx,CCEditBox)