Cocos2d-x中CCEditBox文本輸入框的使用實(shí)例
文本輸入框這個(gè)東西相信大家不論做什么游戲總會(huì)用到吧,今天我們就來(lái)看看這個(gè)東西如何使用。文本輸入框同樣屬于擴(kuò)展庫(kù)中的內(nèi)容,所以你知道怎么做了吧。當(dāng)用戶要在文本框中輸入內(nèi)容,這一系列的過(guò)程我們需要一些函數(shù)的調(diào)用來(lái)獲得我們想要的東西,包含這些函數(shù)的類需要實(shí)現(xiàn)CCEditBoxDelegate這個(gè)接口,下面我們來(lái)看看具體如何使用吧。

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
//需要包含擴(kuò)展庫(kù)
#include "cocos-ext.h"
using namespace cocos2d;
using namespace cocos2d::extension;
//使用CCEditBox必須繼承自CCEditBoxDelegate接口,實(shí)現(xiàn)其的一些函數(shù)
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);
//要實(shí)現(xiàn)的函數(shù)如下
//當(dāng)鍵盤(pán)彈出編輯框獲得焦點(diǎn)時(shí)調(diào)用
virtual void editBoxEditingDidBegin(CCEditBox* editBox);
//當(dāng)鍵盤(pán)消失編輯框失去焦點(diǎn)時(shí)調(diào)用
virtual void editBoxEditingDidEnd(CCEditBox* editBox);
//當(dāng)編輯框文本改變時(shí)調(diào)用
virtual void editBoxTextChanged(CCEditBox* editBox, const std::string& text);
//當(dāng)返回鍵按下時(shí)或者點(diǎn)擊了鍵盤(pán)以外的區(qū)域時(shí)調(diào)用
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();
//第一個(gè)參數(shù)是文本框的大小,第二個(gè)是文本框在正常情況下的背景圖片,第三個(gè)參數(shù)是按下時(shí)候的背景圖片
//第四個(gè)參數(shù)是不可用的時(shí)候的背景圖片,后三個(gè)參數(shù)可以省略
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);
//設(shè)置預(yù)置文本
editBox->setPlaceHolder("please input:");
//設(shè)置文本字體的顏色
editBox->setFontColor(ccc3(255,0,0));
//設(shè)置最大長(zhǎng)度 ,按說(shuō)這個(gè)地方是輸入框文字的長(zhǎng)度,但是在win32上不管用,移植到android的時(shí)候是管用的
editBox->setMaxLength(1);
//setInputMode()設(shè)置輸入類型,可以包括如下的幾種
// kEditBoxInputModeAny: 開(kāi)啟任何文本的輸入鍵盤(pán),包括換行
// kEditBoxInputModeEmailAddr: 開(kāi)啟 郵件地址 輸入類型鍵盤(pán)
// kEditBoxInputModeNumeric: 開(kāi)啟 數(shù)字符號(hào) 輸入類型鍵盤(pán)
// kEditBoxInputModePhoneNumber: 開(kāi)啟 電話號(hào)碼 輸入類型鍵盤(pán)
// kEditBoxInputModeUrl: 開(kāi)啟 URL 輸入類型鍵盤(pán)
// kEditBoxInputModeDecimal: 開(kāi)啟 數(shù)字 輸入類型鍵盤(pán),允許小數(shù)點(diǎn)
// kEditBoxInputModeSingleLine: 開(kāi)啟任何文本的輸入鍵盤(pán),不包括換行
editBox->setInputMode(kEditBoxInputModeAny);
//設(shè)置輸入標(biāo)志,可以有如下的幾種
//kEditBoxInputFlagPassword: 密碼形式輸入
//kEditBoxInputFlagSensitive: 敏感數(shù)據(jù)輸入、存儲(chǔ)輸入方案且預(yù)測(cè)自動(dòng)完成
//kEditBoxInputFlagInitialCapsWord: 每個(gè)單詞首字母大寫(xiě),并且伴有提示
//kEditBoxInputFlagInitialCapsSentence: 第一句首字母大寫(xiě),并且伴有提示
//kEditBoxInputFlagInitialCapsAllCharacters:所有字符自動(dòng)大寫(xiě)
editBox->setInputFlag(kEditBoxInputFlagPassword);
//設(shè)置鍵盤(pán)中return鍵顯示的字符,這個(gè)移植android的時(shí)候沒(méi)有看出來(lái)
editBox->setReturnType(kKeyboardReturnTypeGo);
//包括這些選項(xiàng)
//kKeyboardReturnTypeDefault: 默認(rèn)使用鍵盤(pán)return 類型
//kKeyboardReturnTypeDone: 默認(rèn)使用鍵盤(pán)return類型為“Done”字樣
//kKeyboardReturnTypeSend: 默認(rèn)使用鍵盤(pán)return類型為“Send”字樣
//kKeyboardReturnTypeSearch: 默認(rèn)使用鍵盤(pán)return類型為“Search”字樣
//kKeyboardReturnTypeGo: 默認(rèn)使用鍵盤(pán)return類型為“Go”字樣
//寫(xiě)上這句話的時(shí)候以下的四個(gè)函數(shù)才會(huì)被調(diào)用
editBox->setDelegate(this);
return true;
}
//實(shí)現(xiàn)以下的函數(shù),觀察他們是何時(shí)被調(diào)用的
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);
}
- 剖析iOS開(kāi)發(fā)中Cocos2d-x的內(nèi)存管理相關(guān)操作
- iOS開(kāi)發(fā)中使用cocos2d添加觸摸事件的方法
- cocos2dx骨骼動(dòng)畫(huà)Armature源碼剖析(三)
- cocos2dx骨骼動(dòng)畫(huà)Armature源碼剖析(二)
- cocos2dx骨骼動(dòng)畫(huà)Armature源碼剖析(一)
- Cocos2d-x 3.x入門(mén)教程(二):Node節(jié)點(diǎn)類
- Cocos2d-x 3.x入門(mén)教程(一):基礎(chǔ)概念
- Cocos2d-x中調(diào)用Lua及HelloWorld.lua源碼分解
- Cocos2d-x中使用CCScrollView來(lái)實(shí)現(xiàn)關(guān)卡選擇實(shí)例
- Cocos2d-x中實(shí)現(xiàn)彈出對(duì)話框示例
- Cocos2d-x觸摸事件實(shí)例
- Cocos2d-x人物動(dòng)作類實(shí)例
- 詳解iOS游戲開(kāi)發(fā)中Cocos2D的坐標(biāo)位置關(guān)系
相關(guān)文章
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之 折半查找實(shí)例詳解
這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之 折半查找實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
C語(yǔ)言詳解如何實(shí)現(xiàn)帶頭雙向循環(huán)鏈表
帶頭雙向循環(huán)鏈表:結(jié)構(gòu)最復(fù)雜,一般用在單獨(dú)存儲(chǔ)數(shù)據(jù)。實(shí)際中使用的鏈表數(shù)據(jù)結(jié)構(gòu),都是帶頭雙向循環(huán)鏈表。另外這個(gè)結(jié)構(gòu)雖然結(jié)構(gòu)復(fù)雜,但是使用代碼實(shí)現(xiàn)以后會(huì)發(fā)現(xiàn)結(jié)構(gòu)會(huì)帶來(lái)很多優(yōu)勢(shì),實(shí)現(xiàn)反而簡(jiǎn)單2022-04-04
使用C++制作簡(jiǎn)單的web服務(wù)器(續(xù))
本文承接上文《使用C++制作簡(jiǎn)單的web服務(wù)器》,把web服務(wù)器做的功能稍微強(qiáng)大些,主要增加的功能是從文件中讀取網(wǎng)頁(yè)并返回給客戶端,而不是把網(wǎng)頁(yè)代碼寫(xiě)死在代碼中,有需要的小伙伴來(lái)參考下吧。2015-03-03

