您的位置:首页 > 其它

CCEditBox 编辑框

2013-04-08 19:18 92 查看
新建工程,名为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

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 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(300,
60), sacel9SprY);

            
           
//设置编辑框内的文字
            box->setText("");
           
//获取编辑框内的文字
           
CCLOG("Text:%s",box->getText());

            
           
//设置文本的颜色
            box->setFontColor(ccc3(255,
0, 0));

            

            //当编辑框中没有任何字符的提示
            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.5,
220));
           
addChild(box);

            
           
CCScale9Sprite *sacel9SprG=CCScale9Sprite::create("green_edit.png");
           
CCEditBox *box2=CCEditBox::create(CCSizeMake(300,
60), sacel9SprG);

            box2->setInputFlag(kEditBoxInputFlagPassword);

            box2->setReturnType(kKeyboardReturnTypeGo);
            box2->setMaxLength(12);
            box2->setPlaceHolder("请输入密码");
            box2->setPosition(ccp(size.width*0.5,
120));
           
addChild(box2);

        }
           
break;

            
       
case 2:
        {
           
CCScale9Sprite *sacel9SprG=CCScale9Sprite::create("green_edit.png");
           
CCEditBox *box2=CCEditBox::create(CCSizeMake(300,
60), 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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: