国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

Cocos2d-x中CCEditBox文本輸入框的使用實例

2020-01-26 15:19:21
字體:
來源:轉載
供稿:網友

文本輸入框這個東西相信大家不論做什么游戲總會用到吧,今天我們就來看看這個東西如何使用。文本輸入框同樣屬于擴展庫中的內容,所以你知道怎么做了吧。當用戶要在文本框中輸入內容,這一系列的過程我們需要一些函數的調用來獲得我們想要的東西,包含這些函數的類需要實現CCEditBoxDelegate這個接口,下面我們來看看具體如何使用吧。

#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"http://需要包含擴展庫#include "cocos-ext.h"using namespace cocos2d;using namespace cocos2d::extension;//使用CCEditBox必須繼承自CCEditBoxDelegate接口,實現其的一些函數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);	//要實現的函數如下	//當鍵盤彈出編輯框獲得焦點時調用	virtual void editBoxEditingDidBegin(CCEditBox* editBox);	//當鍵盤消失編輯框失去焦點時調用	virtual void editBoxEditingDidEnd(CCEditBox* editBox);	//當編輯框文本改變時調用	virtual void editBoxTextChanged(CCEditBox* editBox, const std::string& text);	//當返回鍵按下時或者點擊了鍵盤以外的區域時調用	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();	//第一個參數是文本框的大小,第二個是文本框在正常情況下的背景圖片,第三個參數是按下時候的背景圖片	//第四個參數是不可用的時候的背景圖片,后三個參數可以省略	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);	//設置預置文本	editBox->setPlaceHolder("please input:");	//設置文本字體的顏色	editBox->setFontColor(ccc3(255,0,0));	//設置最大長度 ,按說這個地方是輸入框文字的長度,但是在win32上不管用,移植到android的時候是管用的	editBox->setMaxLength(1);	//setInputMode()設置輸入類型,可以包括如下的幾種	//   kEditBoxInputModeAny:     開啟任何文本的輸入鍵盤,包括換行	//   kEditBoxInputModeEmailAddr:  開啟 郵件地址 輸入類型鍵盤	//   kEditBoxInputModeNumeric:   開啟 數字符號 輸入類型鍵盤	//   kEditBoxInputModePhoneNumber: 開啟 電話號碼 輸入類型鍵盤	//   kEditBoxInputModeUrl:     開啟 URL 輸入類型鍵盤	//   kEditBoxInputModeDecimal:   開啟 數字 輸入類型鍵盤,允許小數點	//   kEditBoxInputModeSingleLine: 開啟任何文本的輸入鍵盤,不包括換行	editBox->setInputMode(kEditBoxInputModeAny);	//設置輸入標志,可以有如下的幾種	//kEditBoxInputFlagPassword:        密碼形式輸入	//kEditBoxInputFlagSensitive:        敏感數據輸入、存儲輸入方案且預測自動完成	//kEditBoxInputFlagInitialCapsWord:     每個單詞首字母大寫,并且伴有提示	//kEditBoxInputFlagInitialCapsSentence:   第一句首字母大寫,并且伴有提示	//kEditBoxInputFlagInitialCapsAllCharacters:所有字符自動大寫	editBox->setInputFlag(kEditBoxInputFlagPassword);	//設置鍵盤中return鍵顯示的字符,這個移植android的時候沒有看出來  editBox->setReturnType(kKeyboardReturnTypeGo);  //包括這些選項  //kKeyboardReturnTypeDefault: 默認使用鍵盤return 類型	//kKeyboardReturnTypeDone:   默認使用鍵盤return類型為“Done”字樣	//kKeyboardReturnTypeSend:   默認使用鍵盤return類型為“Send”字樣	//kKeyboardReturnTypeSearch:  默認使用鍵盤return類型為“Search”字樣	//kKeyboardReturnTypeGo:    默認使用鍵盤return類型為“Go”字樣	//寫上這句話的時候以下的四個函數才會被調用	editBox->setDelegate(this);  return true;}//實現以下的函數,觀察他們是何時被調用的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);}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 田林县| 蒙阴县| 贡山| 阿巴嘎旗| 郴州市| 深泽县| 大城县| 宿松县| 吉安市| 定陶县| 嘉荫县| 临清市| 金乡县| 桐柏县| 武安市| 辽宁省| 巫溪县| 莒南县| 嘉定区| 孟村| 长乐市| 台江县| 乌兰察布市| 高邑县| 海安县| 灵丘县| 阿瓦提县| 井冈山市| 湖口县| 土默特右旗| 盘锦市| 张家口市| 赞皇县| 鹰潭市| 紫阳县| 平舆县| 忻城县| 楚雄市| 襄城县| 大邑县| 黄石市|