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

来源:互联网 发布:单片机c语言教程pdf 编辑:程序博客网 时间:2024/05/28 23:23

梅沙小子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.csdn.net/u010105970/article/details/39937827


0 0
原创粉丝点击