Cocos2d-x中CCEditBox文本输入框的使用实例

2025-05-27 0 83

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

Cocos2d-x中CCEditBox文本输入框的使用实例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44
#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__

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

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

}

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Cocos2d-x中CCEditBox文本输入框的使用实例 https://www.kuaiidc.com/75865.html

相关文章

发表评论
暂无评论