Cocos2d-x中CCEditBox文本輸入框的使用實例
更新時間:2014年09月12日 10:40:56 投稿:junjie
這篇文章主要介紹了Cocos2d-x中CCEditBox文本輸入框的使用實例,本文在代碼中用大量注釋講解了CCEditBox的使用方法,需要的朋友可以參考下
文本輸入框這個東西相信大家不論做什么游戲總會用到吧,今天我們就來看看這個東西如何使用。文本輸入框同樣屬于擴(kuò)展庫中的內(nèi)容,所以你知道怎么做了吧。當(dāng)用戶要在文本框中輸入內(nèi)容,這一系列的過程我們需要一些函數(shù)的調(diào)用來獲得我們想要的東西,包含這些函數(shù)的類需要實現(xiàn)CCEditBoxDelegate這個接口,下面我們來看看具體如何使用吧。
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" //需要包含擴(kuò)展庫 #include "cocos-ext.h" using namespace cocos2d; using namespace cocos2d::extension; //使用CCEditBox必須繼承自CCEditBoxDelegate接口,實現(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); //要實現(xiàn)的函數(shù)如下 //當(dāng)鍵盤彈出編輯框獲得焦點時調(diào)用 virtual void editBoxEditingDidBegin(CCEditBox* editBox); //當(dāng)鍵盤消失編輯框失去焦點時調(diào)用 virtual void editBoxEditingDidEnd(CCEditBox* editBox); //當(dāng)編輯框文本改變時調(diào)用 virtual void editBoxTextChanged(CCEditBox* editBox, const std::string& text); //當(dāng)返回鍵按下時或者點擊了鍵盤以外的區(qū)域時調(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(); //第一個參數(shù)是文本框的大小,第二個是文本框在正常情況下的背景圖片,第三個參數(shù)是按下時候的背景圖片 //第四個參數(shù)是不可用的時候的背景圖片,后三個參數(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è)置最大長度 ,按說這個地方是輸入框文字的長度,但是在win32上不管用,移植到android的時候是管用的 editBox->setMaxLength(1); //setInputMode()設(shè)置輸入類型,可以包括如下的幾種 // kEditBoxInputModeAny: 開啟任何文本的輸入鍵盤,包括換行 // kEditBoxInputModeEmailAddr: 開啟 郵件地址 輸入類型鍵盤 // kEditBoxInputModeNumeric: 開啟 數(shù)字符號 輸入類型鍵盤 // kEditBoxInputModePhoneNumber: 開啟 電話號碼 輸入類型鍵盤 // kEditBoxInputModeUrl: 開啟 URL 輸入類型鍵盤 // kEditBoxInputModeDecimal: 開啟 數(shù)字 輸入類型鍵盤,允許小數(shù)點 // kEditBoxInputModeSingleLine: 開啟任何文本的輸入鍵盤,不包括換行 editBox->setInputMode(kEditBoxInputModeAny); //設(shè)置輸入標(biāo)志,可以有如下的幾種 //kEditBoxInputFlagPassword: 密碼形式輸入 //kEditBoxInputFlagSensitive: 敏感數(shù)據(jù)輸入、存儲輸入方案且預(yù)測自動完成 //kEditBoxInputFlagInitialCapsWord: 每個單詞首字母大寫,并且伴有提示 //kEditBoxInputFlagInitialCapsSentence: 第一句首字母大寫,并且伴有提示 //kEditBoxInputFlagInitialCapsAllCharacters:所有字符自動大寫 editBox->setInputFlag(kEditBoxInputFlagPassword); //設(shè)置鍵盤中return鍵顯示的字符,這個移植android的時候沒有看出來 editBox->setReturnType(kKeyboardReturnTypeGo); //包括這些選項 //kKeyboardReturnTypeDefault: 默認(rèn)使用鍵盤return 類型 //kKeyboardReturnTypeDone: 默認(rèn)使用鍵盤return類型為“Done”字樣 //kKeyboardReturnTypeSend: 默認(rèn)使用鍵盤return類型為“Send”字樣 //kKeyboardReturnTypeSearch: 默認(rèn)使用鍵盤return類型為“Search”字樣 //kKeyboardReturnTypeGo: 默認(rèn)使用鍵盤return類型為“Go”字樣 //寫上這句話的時候以下的四個函數(shù)才會被調(diào)用 editBox->setDelegate(this); return true; } //實現(xiàn)以下的函數(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開發(fā)中Cocos2d-x的內(nèi)存管理相關(guān)操作
- iOS開發(fā)中使用cocos2d添加觸摸事件的方法
- cocos2dx骨骼動畫Armature源碼剖析(三)
- cocos2dx骨骼動畫Armature源碼剖析(二)
- cocos2dx骨骼動畫Armature源碼剖析(一)
- Cocos2d-x 3.x入門教程(二):Node節(jié)點類
- Cocos2d-x 3.x入門教程(一):基礎(chǔ)概念
- Cocos2d-x中調(diào)用Lua及HelloWorld.lua源碼分解
- Cocos2d-x中使用CCScrollView來實現(xiàn)關(guān)卡選擇實例
- Cocos2d-x中實現(xiàn)彈出對話框示例
- Cocos2d-x觸摸事件實例
- Cocos2d-x人物動作類實例
- 詳解iOS游戲開發(fā)中Cocos2D的坐標(biāo)位置關(guān)系
相關(guān)文章
記逆向小白的第一次vbsedit 9爆破及內(nèi)存補(bǔ)丁制作過程
這篇文章主要介紹了記逆向小白的第一次vbsedit 9爆破及內(nèi)存補(bǔ)丁制作過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04C標(biāo)準(zhǔn)庫<assert.h>的實現(xiàn)詳解
這篇文章主要介紹了C標(biāo)準(zhǔn)庫<assert.h>的實現(xiàn),主要包括了<assert.h>的基本概念、實現(xiàn)及用法等,需要的朋友可以參考下2014-09-09