CCEditBox 编辑框

来源:互联网 发布:淘宝管控记录怎么销除 编辑:程序博客网 时间:2024/05/21 13:56

原文链接:http://blog.csdn.net/cloud95/article/details/8773470


新建工程,名为testEdit.

修改HelloWorld.h

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"

#include "cocos-ext.h"

using namespace cocos2d::extension;

class HelloWorld : public cocos2d::CCLayer,public CCEditBoxDelegate

{

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();

    CREATE_FUNC(HelloWorld);

    

    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 // __HELLOWORLD_SCENE_H__

修改HelloWorld.cpp

#include "HelloWorldScene.h"

#include "SimpleAudioEngine.h"


using namespace cocos2d;

using namespace CocosDenshion;

#define testflag 2

CCSceneHelloWorld::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 instance

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

    if ( !CCLayer::init() )

    {

        return false;

    }

    switch (testflag) {

        case 1:

        {

            CCSize size=CCDirector::sharedDirector()->getWinSize();

            CCScale9Sprite *sacel9SprY= CCScale9Sprite::create("yellow_edit.png");

            CCEditBox *box=CCEditBox::create(CCSizeMake(30060), sacel9SprY);

            

            //设置编辑框内的文字

            box->setText("");

            //获取编辑框内的文字

            CCLOG("Text:%s",box->getText());

            

            //设置文本的颜色

            box->setFontColor(ccc3(25500));

            

            //当编辑框中没有任何字符的提示

            box->setPlaceHolder("请输入帐号");

            CCLOG("PlaceHolder:%s",box->getPlaceHolder());

            

            //最大输入文本长度

            box->setMaxLength(10);

            CCLOG("Length:%i",box->getMaxLength());

            

            //设置输入模式

            box->setInputMode(kEditBoxInputModeAny);

            

            /**

             //      kEditBoxInputModeAny:         开启任何文本的输入键盘,包括换行

             //      kEditBoxInputModeEmailAddr:   开启 邮件地址 输入类型键盘

             //      kEditBoxInputModeNumeric:     开启 数字符号 输入类型键盘

             //      kEditBoxInputModePhoneNumber: 开启 电话号码 输入类型键盘

             //      kEditBoxInputModeUrl:         开启 URL 输入类型键盘

             //      kEditBoxInputModeDecimal:     开启 数字 输入类型键盘,允许小数点

             //      kEditBoxInputModeSingleLine:  开启任何文本的输入键盘,不包括换行

             //

             */

            

            //设置输入类型

            box->setInputFlag(kEditBoxInputFlagSensitive);

            /**

             //      kEditBoxInputFlagPassword:  密码形式输入

             //      kEditBoxInputFlagSensitive: 敏感数据输入、存储输入方案且预测自动完成

             //      kEditBoxInputFlagInitialCapsWord: 每个单词首字母大写,并且伴有提示

             //      kEditBoxInputFlagInitialCapsSentence: 第一句首字母大写,并且伴有提示

             //      kEditBoxInputFlagInitialCapsAllCharacters: 所有字符自动大写

             //     */

            

            

            

            

            //设置返回类型

            box->setReturnType(kKeyboardReturnTypeDone);

            /**

             //      kKeyboardReturnTypeDefault:  默认使用键盘return 类型

             //      kKeyboardReturnTypeDone:     默认使用键盘return类型为“Done”字样

             //      kKeyboardReturnTypeSend:     默认使用键盘return类型为“Send”字样

             //      kKeyboardReturnTypeSearch:   默认使用键盘return类型为“Search”字样

             //      kKeyboardReturnTypeGo:       默认使用键盘return类型为“Go”字样

             //      */

            box->setPosition(ccp(size.width*0.5220));

            addChild(box);

            

            CCScale9Sprite *sacel9SprG=CCScale9Sprite::create("green_edit.png");

            CCEditBox *box2=CCEditBox::create(CCSizeMake(30060), sacel9SprG);

            box2->setInputFlag(kEditBoxInputFlagPassword);

            box2->setReturnType(kKeyboardReturnTypeGo);

            box2->setMaxLength(12);

            box2->setPlaceHolder("请输入密码");

            box2->setPosition(ccp(size.width*0.5120));

            addChild(box2);


        }

            break;

            

        case 2:

        {

            CCScale9Sprite *sacel9SprG=CCScale9Sprite::create("green_edit.png");

            CCEditBox *box2=CCEditBox::create(CCSizeMake(30060), sacel9SprG);

            box2->setPlaceHolder("Delegate");

            box2->setPosition(ccp(220,120));            

            addChild(box2);

            box2->setDelegate(this);


        }

            break;

    }

        return true;

}



void HelloWorld::editBoxEditingDidBegin(CCEditBox *editBox)

{

    CCLOG("start edit");

}

void HelloWorld::editBoxEditingDidEnd(CCEditBox *editBox) 

{

     CCLOG("end edit");

}

void HelloWorld::editBoxTextChanged(CCEditBox *editBox,const std::string &text)

{

     CCLOG("textchanged");

}

void HelloWorld::editBoxReturn(CCEditBox *editBox)

{

     CCLOG("editboxreturn");

}

原创粉丝点击