Cocos2d-x中使用CCTextFieldTTF的簡單應用顯示文本和彈出軟鍵盤

梅沙小子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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章