展会信息港展会大全

Cocos2d-x中CCEditBox文本输入框的使用实例
来源:互联网   发布日期:2015-09-28 10:41:18   浏览:3936次  

导读: 文本输入框这个东西相信大家不论做什么游戏总会用到吧,今天我们就来看看这个东西如何使用。文本输入框同样属于扩展库中的内容,所以你知道怎么做了吧。当用户要在文本框中输入内容,这一系列的过程我们需要一...

文本输入框这个东西相信大家不论做什么游戏总会用到吧,今天我们就来看看这个东西如何使用。文本输入框同样属于扩展库中的内容,所以你知道怎么做了吧。当用户要在文本框中输入内容,这一系列的过程我们需要一些函数的调用来获得我们想要的东西,包含这些函数的类需要实现CCEditBoxDelegate这个接口,下面我们来看看具体如何使用吧。

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

//需要包含扩展库

#include "cocos-ext.h"

using namespace cocos2d;

using namespace cocos2d::extension;

//使用CCEditBox必须继承自CCEditBoxDelegate接口,实现其的一些函数

class HelloWorld : public cocos2d::CCLayer,public CCEditBoxDelegate

{

public:

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone

virtual bool init();

// there's no 'id' in cpp, so we recommend returning the class instance pointer

static cocos2d::CCScene* scene();

// implement the "static node()" method manually

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

private:

CCSize m_size;

CCEditBox * editBox;

};

#endif // __HELLOWORLD_SCENE_H__

bool HelloWorld::init()

{

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

// 1. super init first

if ( !CCLayer::init() )

{

return false;

}

this->m_size = CCDirector::sharedDirector()->getVisibleSize();

//第一个参数是文本框的大小,第二个是文本框在正常情况下的背景图片,第三个参数是按下时候的背景图片

//第四个参数是不可用的时候的背景图片,后三个参数可以省略

editBox = CCEditBox::create(CCSize(300,40),

CCScale9Sprite::create("9.9.png"),

CCScale9Sprite::create("8.9.png"));

editBox->setPosition(ccp(m_size.width/2,m_size.height/2));

this->addChild(editBox);

//设置预置文本

editBox->setPlaceHolder("please input:");

//设置文本字体的颜色

editBox->setFontColor(ccc3(255,0,0));

//设置最大长度 ,按说这个地方是输入框文字的长度,但是在win32上不管用,移植到android的时候是管用的

editBox->setMaxLength(1);

//setInputMode()设置输入类型,可以包括如下的几种

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

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

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

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

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

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

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

editBox->setInputMode(kEditBoxInputModeAny);

//设置输入标志,可以有如下的几种

//kEditBoxInputFlagPassword:密码形式输入

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

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

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

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

editBox->setInputFlag(kEditBoxInputFlagPassword);

//设置键盘中return键显示的字符,这个移植android的时候没有看出来

editBox->setReturnType(kKeyboardReturnTypeGo);

//包括这些选项

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

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

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

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

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

//写上这句话的时候以下的四个函数才会被调用

editBox->setDelegate(this);

return true;

}

//实现以下的函数,观察他们是何时被调用的

void HelloWorld::editBoxEditingDidBegin(CCEditBox * editBox)

{

CCLog("begin!");

CCLabelTTF * ttf = CCLabelTTF::create("begin","",24);

ttf->setPosition(ccp(m_size.width/4,m_size.height*1/5));

this->addChild(ttf);

}

void HelloWorld::editBoxEditingDidEnd(CCEditBox * editBox)

{

CCLog("end!");

CCLabelTTF * ttf = CCLabelTTF::create("end","",24);

ttf->setPosition(ccp(m_size.width/4,m_size.height*4/5));

this->addChild(ttf);

}

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

{

CCLog("textChanged!");

CCLabelTTF * ttf = CCLabelTTF::create("textChanged!","",24);

ttf->setPosition(ccp(m_size.width/4,m_size.height*3/5));

this->addChild(ttf);

}

void HelloWorld::editBoxReturn(CCEditBox * editBox)

{

CCLog("return");

CCLabelTTF * ttf = CCLabelTTF::create("return","",24);

ttf->setPosition(ccp(m_size.width/4,m_size.height*2/5));

this->addChild(ttf);

char * str = (char *)this->editBox->getText();

CCLabelTTF * text = CCLabelTTF::create(str,"",24);

text->setPosition(ccp(m_size.width/2,m_size.height*2/5));

this->addChild(text);

}

赞助本站

人工智能实验室
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港