Cocos2d-x中使用CCTextFieldTTF的简单应用显示文本和弹出软键盘

本文介绍如何使用Cocos2d-x中的CCTextFieldTTF类实现在Android平台上的文本输入功能,包括普通文本输入、密码输入及利用九宫格图片美化输入框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

梅沙小子2014-10-14 14:44:361250 次阅读

本文就以一个简单的文本测试Cocos2d-x在Android平台上开发的效果,需要用到CCTextFieldTTF类,CCTextFieldTTF是一个显示文本控件的类用于输入文本和现实文本类似于Windows编程中的Static控件和Edit控件。


程序实例:使用TextFieldTTF类创建一个文本,触摸文本弹出软键盘,并且可以通过软键盘向TextFieldTTF中输入文字

首先创建一个TextFieldTTF.h的头文件,在头文件中添加下面的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef __TextFieldTTF_H__
#define __TextFieldTTF_H__
 
#include "cocos2d.h"
USING_NS_CC;
 
class  TextFieldTTF :  public  CCLayer
{
public :
      bool  init();  
 
     static  CCScene* scene();
 
     //用于处理触摸事件
     bool  ccTouchBegan(CCTouch*, CCEvent*);
 
     //用于在程序中创建一个文本控件
     CCTextFieldTTF* textEdit;
 
     CREATE_FUNC(TextFieldTTF);
};
 
#endif // __HELLOWORLD_SCENE_H__

然后在TextFieldTTF.cpp中添加下面的代码

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
#include "TextFieldTTF.h"
 
CCScene* TextFieldTTF::scene()
{
     CCScene* scene = CCScene::create();
     
     TextFieldTTF* layer = TextFieldTTF::create();
 
     scene->addChild(layer);
 
     return  scene;
}
 
 
bool  TextFieldTTF::init()
{
     //初始化父类层
     CCLayer::init();
 
     //得到窗口的尺寸
     CCSize winSize = CCDirector::sharedDirector()->getWinSize();
 
     //创建文本框
     //第一个参数:文本框中显示的内容
     //第二个参数:字体
     //第三个参数:文本的大小
     textEdit = CCTextFieldTTF::textFieldWithPlaceHolder( "Please input your name:" ,
              "Arial" , 36);
 
     //设置文本框的位置
     textEdit->setPosition(ccp(winSize.width / 2, winSize.height / 2));
 
     //添加文本框到层上
     addChild(textEdit);
 
     //当触摸到控件的时候弹出软键盘
     setTouchMode(kCCTouchesOneByOne);
     setTouchEnabled( true );
 
     return  true ;
}
 
bool  TextFieldTTF::ccTouchBegan(CCTouch* touch, CCEvent* ev)
{
     //用于判断是否点中了控件
     bool  isClicked = textEdit->boundingBox().containsPoint(touch->getLocation());
 
     //如果点中了控件
     if (isClicked)
     {
         //弹出软键盘
         textEdit->attachWithIME();
     }
 
     //表示接受触摸消息
     return  true ;
}

程序执行结果:

1413268567179980.png

在Windows下单击“Please input your name: ”会没有反应,因为Windows下没有软键盘。

程序移值到Android下的执行结果:

1413268595771217.png

触摸“Please input your name :”后弹出软键盘

20141013104353663.png

使用软键盘输入一段文字后:

20141013104325359.png

选择完成后文字显示在控件上

1413268875237131.png


程序实例:TextFieldTTF实现输入密码

将TextFieldTTF.cpp文件中的代码改成下面的代码

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
#include "TextFieldTTF.h"
 
CCScene* TextFieldTTF::scene()
{
     CCScene* scene = CCScene::create();
     
     TextFieldTTF* layer = TextFieldTTF::create();
 
     scene->addChild(layer);
 
     return  scene;
}
 
 
bool  TextFieldTTF::init()
{
     //初始化父类层
     CCLayer::init();
 
     //得到窗口的尺寸
     CCSize winSize = CCDirector::sharedDirector()->getWinSize();
 
     //创建文本框
     textEdit = CCTextFieldTTF::textFieldWithPlaceHolder( "Please input your name:" ,
              "Arial" , 36);
 
     //设置文本框的位置
     textEdit->setPosition(ccp(winSize.width / 2, winSize.height / 2));
 
     //添加文本框到层上
     addChild(textEdit);
 
     //输入密码
     textEdit->setSecureTextEntry( true );
 
     //注册触摸函数,实现当触摸到控件的时候,弹出软键盘
     setTouchMode(kCCTouchesOneByOne);
     setTouchEnabled( true );
 
     return  true ;
    
}
 
bool  TextFieldTTF::ccTouchBegan(CCTouch* touch, CCEvent* ev)
{
     //用于判断是否点中了控件
     bool  isClicked = textEdit->boundingBox().containsPoint(touch->getLocation());
 
     //如果点中了控件
     if (isClicked)
     {
         //弹出软键盘
         textEdit->attachWithIME();
     }
 
     //表示接受触摸消息
     return  true ;
}

程序移值到Android下的执行结果:程序移值到Android下的执行结果:

20141013105551935.png

触摸“Please input your name :”后弹出软键盘

20141013105642885.png

通过软键盘输入一段字符

20141013105539750.png

选择完成后字符以密码的形式显示在控件上

20141013105756937.png


程序实例:使用九位图美化控件

在工程目录下的Resource文件夹中放一张九位图

20141013110226453.png

将TextFieldTTF.cpp文件中的代码改成下面的代码

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
#include "TextFieldTTF.h"
 
CCScene* TextFieldTTF::scene()
{
     CCScene* scene = CCScene::create();
     
     TextFieldTTF* layer = TextFieldTTF::create();
 
     scene->addChild(layer);
 
     return  scene;
}
 
 
bool  TextFieldTTF::init()
{
     //初始化父类层
     CCLayer::init();
 
     //得到窗口的尺寸
     CCSize winSize = CCDirector::sharedDirector()->getWinSize();
 
     //创建文本框
     textEdit = CCTextFieldTTF::textFieldWithPlaceHolder( "Please input your name:" ,
              "Arial" , 36);
 
     //设置文本框的位置
     textEdit->setPosition(ccp(winSize.width / 2, winSize.height / 2));
 
     //添加文本框到层上
     addChild(textEdit);
 
     
     //给控件增加背景(添加一张九位图)
     CCScale9Sprite* bg = CCScale9Sprite::create( "green_edit.png" );
     
     //将九位图添加到控件上
     textEdit->addChild(bg);
 
     //设置描点的位置
     bg->setAnchorPoint(ccp(0,0));
 
     //设置九位图的位置
     bg->setPosition(ccp(0,0));
 
     //将九位图的尺寸设置成控件的尺寸一样大
     bg->setContentSize(textEdit->boundingBox().size);
 
     //先显示九位图后显示控件
     bg->setZOrder(-1);
 
     //注册触摸函数,实现当触摸到控件的时候,弹出软键盘
     setTouchMode(kCCTouchesOneByOne);
     setTouchEnabled( true );
 
     return  true ;
    
}
 
bool  TextFieldTTF::ccTouchBegan(CCTouch* touch, CCEvent* ev)
{
     //用于判断是否点中了控件
     bool  isClicked = textEdit->boundingBox().containsPoint(touch->getLocation());
 
     //如果点中了控件
     if (isClicked)
     {
         //弹出软键盘
         textEdit->attachWithIME();
     }
 
     //表示接受触摸消息
     return  true ;
}

程序移值到Android下的执行结果:

20141013110838162.png

触摸“Please input your name :”后弹出软键盘

20141013111009391.png

使用软键盘输入一段文字

20141013111114739.png

选择完成后文字显示在控件上

20141013111017000.png


来源网址:http://blog.youkuaiyun.com/u010105970/article/details/39937827


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值