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);
//包括這些選項
//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);
}
- 剖析iOS開發(fā)中Cocos2d-x的內(nèi)存管理相關(guān)操作
- iOS開發(fā)中使用cocos2d添加觸摸事件的方法
- cocos2dx骨骼動畫Armature源碼剖析(三)
- cocos2dx骨骼動畫Armature源碼剖析(二)
- cocos2dx骨骼動畫Armature源碼剖析(一)
- Cocos2d-x 3.x入門教程(二):Node節(jié)點(diǎn)類
- Cocos2d-x 3.x入門教程(一):基礎(chǔ)概念
- Cocos2d-x中調(diào)用Lua及HelloWorld.lua源碼分解
- Cocos2d-x中使用CCScrollView來實(shí)現(xiàn)關(guān)卡選擇實(shí)例
- Cocos2d-x中實(shí)現(xiàn)彈出對話框示例
- Cocos2d-x觸摸事件實(shí)例
- Cocos2d-x人物動作類實(shí)例
- 詳解iOS游戲開發(fā)中Cocos2D的坐標(biāo)位置關(guān)系
相關(guān)文章
記逆向小白的第一次vbsedit 9爆破及內(nèi)存補(bǔ)丁制作過程
這篇文章主要介紹了記逆向小白的第一次vbsedit 9爆破及內(nèi)存補(bǔ)丁制作過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
C標(biāo)準(zhǔn)庫<assert.h>的實(shí)現(xiàn)詳解
這篇文章主要介紹了C標(biāo)準(zhǔn)庫<assert.h>的實(shí)現(xiàn),主要包括了<assert.h>的基本概念、實(shí)現(xiàn)及用法等,需要的朋友可以參考下2014-09-09
C++實(shí)現(xiàn)簡易選課系統(tǒng)代碼分享
這篇文章主要介紹了C++實(shí)現(xiàn)簡易選課系統(tǒng)及實(shí)現(xiàn)代碼的分享,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你有所幫助2022-01-01
C語言 實(shí)現(xiàn)輸入任意多個整數(shù)
這篇文章主要介紹了C語言 實(shí)現(xiàn)輸入任意多個整數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
C++對象的淺復(fù)制和深復(fù)制詳解及簡單實(shí)例
這篇文章主要介紹了C++對象的淺復(fù)制和深復(fù)制詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
一篇文章教你自己動手實(shí)現(xiàn)C語言庫函數(shù)
這篇文章主要介紹了C語言庫函數(shù)的相關(guān)資料,小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2021-09-09

