Cocos2d-x中CCEditBox文本輸入框的使用實(shí)例
文本輸入框這個東西相信大家不論做什么游戲總會用到吧,今天我們就來看看這個東西如何使用。文本輸入框同樣屬于擴(kuò)展庫中的內(nèi)容,所以你知道怎么做了吧。當(dāng)用戶要在文本框中輸入內(nèi)容,這一系列的過程我們需要一些函數(shù)的調(diào)用來獲得我們想要的東西,包含這些函數(shù)的類需要實(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接口,實(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)鍵盤彈出編輯框獲得焦點(diǎn)時調(diào)用 virtual void editBoxEditingDidBegin(CCEditBox* editBox); //當(dāng)鍵盤消失編輯框失去焦點(diǎn)時調(diào)用 virtual void editBoxEditingDidEnd(CCEditBox* editBox); //當(dāng)編輯框文本改變時調(diào)用 virtual void editBoxTextChanged(CCEditBox* editBox, const std::string& text); //當(dāng)返回鍵按下時或者點(diǎn)擊了鍵盤以外的區(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ù)點(diǎn) // kEditBoxInputModeSingleLine: 開啟任何文本的輸入鍵盤,不包括換行 editBox->setInputMode(kEditBoxInputModeAny); //設(shè)置輸入標(biāo)志,可以有如下的幾種 //kEditBoxInputFlagPassword: 密碼形式輸入 //kEditBoxInputFlagSensitive: 敏感數(shù)據(jù)輸入、存儲輸入方案且預(yù)測自動完成 //kEditBoxInputFlagInitialCapsWord: 每個單詞首字母大寫,并且伴有提示 //kEditBoxInputFlagInitialCapsSentence: 第一句首字母大寫,并且伴有提示 //kEditBoxInputFlagInitialCapsAllCharacters:所有字符自動大寫 editBox->setInputFlag(kEditBoxInputFlagPassword); //設(shè)置鍵盤中return鍵顯示的字符,這個移植android的時候沒有看出來 editBox->setReturnType(kKeyboardReturnTypeGo); //包括這些選項(xiàng) //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; } //實(shí)現(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); }
上一篇:Cocos2d-x UI開發(fā)之菜單類使用實(shí)例
欄 目:C語言
下一篇:Cocos2d-x Schedule定時器的使用實(shí)例
本文標(biāo)題:Cocos2d-x中CCEditBox文本輸入框的使用實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3379.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進(jìn)程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)
- 01-10C++大數(shù)模板(推薦)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)