Cocos2d-x_CCEditBox(编辑框类)介绍

前端之家收集整理的这篇文章主要介绍了Cocos2d-x_CCEditBox(编辑框类)介绍前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//
// HelloWorldScene.h
//

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

class HelloWorld : public cocos2d::CCLayer,public extension::CCEditBoxDelegate  // extension::CCEditBoxDelegate
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();

    CREATE_FUNC(HelloWorld);

    // 必须重写和定义CCEditBoxDelegate的协议,要不然CREATE_FUNC宏会报错
    virtual void editBoxEditingDidBegin(CCEditBox* editBox);
    virtual void editBoxEditingDidEnd(CCEditBox* editBox);
    virtual void editBoxTextChanged(CCEditBox* editBox,const std::string& text);
    virtual void editBoxReturn(CCEditBox* editBox);
};

#endif

//
// HelloWorldScene.cpp
//

#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 winSize = CCDirector::sharedDirector()->getWinSize();

    // 账号输入框
    CCScale9Sprite *scale9Spr = CCScale9Sprite::create("yellow_edit.png");
    CCEditBox *userBox = CCEditBox::create(CCSizeMake(300,60),scale9Spr);
    userBox->setPosition(ccp(winSize.width*0.5,220));
    userBox->setText("");
    userBox->setFontColor(ccc3(255,0));
    userBox->setPlaceHolder("请输入账号");
    userBox->setMaxLength(10);
    userBox->setInputFlag(kEditBoxInputFlagSensitive);
    userBox->setInputMode(kEditBoxInputModePhoneNumber);
    userBox->setReturnType(kKeyboardReturnTypeDone);
    userBox->setDelegate(this);
    this->addChild(userBox);
    
    // 密码输入框
    CCScale9Sprite *scale9SprG = CCScale9Sprite::create("green_edit.png");
    CCEditBox *pwdBox = CCEditBox::create(CCSizeMake(300,scale9SprG);
    pwdBox->setPosition(ccp(winSize.width*0.5,120));
    pwdBox->setText("");
    pwdBox->setFontColor(ccc3(255,0));
    pwdBox->setPlaceHolder("请输入密码");
    pwdBox->setMaxLength(12);
    pwdBox->setInputFlag(kEditBoxInputFlagPassword);
    pwdBox->setInputMode(kEditBoxInputModeAny);
    pwdBox->setReturnType(kKeyboardReturnTypeGo);
    pwdBox->setDelegate(this);
    this->addChild(pwdBox);

    return true;
}

void HelloWorld::editBoxEditingDidBegin(cocos2d::extension::CCEditBox *editBox)
{
    CCLOG("开始编辑");
}

void HelloWorld::editBoxEditingDidEnd(cocos2d::extension::CCEditBox *editBox)
{
    CCLOG("结束编辑");
}

void HelloWorld::editBoxTextChanged(cocos2d::extension::CCEditBox *editBox,const std::string &text)
{
    CCLOG("文本改变:%s",text.c_str());
}

void HelloWorld::editBoxReturn(cocos2d::extension::CCEditBox *editBox)
{
    CCLOG("触发Return按钮");
}
原文链接:https://www.f2er.com/cocos2dx/346775.html

猜你在找的Cocos2d-x相关文章