a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > 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);}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 久久99精品视频 | 99这里只有精品视频 | 免费观看黄a一级视频 | 久久国产成人午夜av影院宅 | 久草视频网址 | 蜜桃视频精品 | 中文字幕第二十六页页 | 黄色毛片观看 | 欧美激情自拍偷拍 | 日本精品视频网站 | 国产伦精品一区二区三毛 | 免费久久精品 | 久久精品免费 | 成人黄色在线观看 | 国产精品久久久久久久久久免费 | 精品国产乱码久久久久久1区2区 | 成人精品在线 | 久久精品视频网 | 色综合一区 | 久久精品久久综合 | 91 在线观看| 欧美国产日韩在线观看 | 嫩草91| 欧美成人在线网站 | 成人免费视频观看视频 | 国产成人免费网站 | 精品国产高清一区二区三区 | 在线看片成人 | 日本黄色毛片 | 日本美女黄网站 | 色婷综合网 | av国产精品毛片一区二区小说 | 日韩欧美中文在线 | 久在线 | 日韩中文字幕在线视频 | 国产精品久久久久久久裸模 | 国产在线免费 | 久久精品性 | 在线亚洲精品 | 欧美一级淫片007 | 免费高潮视频95在线观看网站 |