```
#include"cocos2d.h"
#include"cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace cocos2d::ui;
```
注:因?yàn)榘姹靖В琫ditbox原本是在cocos-ext中倦青,現(xiàn)行版本則更換到了ui中袋励,所以以上頭文件和名空間根據(jù)自己的版本選擇(新版本使用ext也可正常使用,原因見下圖,但不建議)
如下叫挟,使用EditBox的類要繼承ui::EditBoxDelegate
(可使用using namespace cocos2d::ui以省略u(píng)i::,本人項(xiàng)目因?yàn)樾枰褂玫絜xt所以為了避免混淆添加了ui::)
class HomeEditBox : public Node,ui::EditBoxDelegate
在類中要聲明并實(shí)現(xiàn)以下函數(shù):
virtual void editBoxEditingDidBegin(ui::EditBox* editBox);
virtual void editBoxEditingDidEnd(ui::EditBox* editBox);
virtual void editBoxTextChanged(ui::EditBox* editBox,conststd::string& text);//編輯框內(nèi)容改變
virtual void editBoxReturn(ui::EditBox* editBox);//點(diǎn)擊鍵盤回車/確認(rèn)按鈕
m_editBox=ui::EditBox::create(Size(170,80),ui::Scale9Sprite::create("editBoxBg.png"));
//size為輸入框大小,第二個(gè)參數(shù)為輸入框背景,使用Scale9Sprite
m_editBox->setFontSize(40);//輸入內(nèi)容字體大小
m_editBox->setFontColor(Color3B::Black);//輸入內(nèi)容字體顏色
m_editBox->setPlaceHolder("Please enter a number:");//輸入框提示內(nèi)容袖肥,當(dāng)輸入內(nèi)容為空時(shí)顯示,如果內(nèi)容不為空則不顯示
m_editBox->setPlaceholderFontColor(Color3B::GRAY);//提示內(nèi)容字體顏色
m_editBox->setMaxLength(5);//輸入內(nèi)容長(zhǎng)度
m_editBox->setInputMode(ui::EditBox::InputMode::DECIMAL);//輸入鍵盤模式
/*
enumclassInputMode
{
/*
* The user is allowed to enter any text, including line breaks.普通鍵盤 不包含“@” “.”等特殊字符
*/
ANY,
/*
* The user is allowed to enter an e-mail address.郵件地址 包含a-z “ @ ” “ . ”等
*/
EMAIL_ADDRESS,
/**
* The user is allowed to enter an integer value.數(shù)字 包含0-9 “.” delete
*/
NUMERIC,
/**
* The user is allowed to enter a phone number.手機(jī)號(hào)包含0-9 “ * ” “ # ”
*/
PHONE_NUMBER,
/**
* The user is allowed to enter a URL.鏈接地址
*/
URL,
/**
* The user is allowed to enter a real number value.
* This extends kEditBoxInputModeNumeric by allowing a decimal point.小數(shù)
*/
DECIMAL,
/**
* The user is allowed to enter any text, except for line breaks.單行
*/
SINGLE_LINE,
};
*/
m_editBox->setInputFlag(ui::EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS);
//輸入模式
/*
enumclassInputFlag
{
/**
* Indicates that the text entered is confidential data that should be
* obscured whenever possible. This implies EDIT_BOX_INPUT_FLAG_SENSITIVE.
密碼模式振劳,以“ * ”代替輸入內(nèi)容
*/
PASSWORD,
/**
* Indicates that the text entered is sensitive data that the
* implementation must never store into a dictionary or table for use
* in predictive, auto-completing, or other accelerated input schemes.
* A credit card number is an example of sensitive data.
敏感模式 ?輸入內(nèi)容不會(huì)存儲(chǔ)于詞典或者用于聯(lián)想輸入椎组、快捷輔助輸入等
*/
SENSITIVE,
/**
* This flag is a hint to the implementation that during text editing,
* the initial letter of each word should be capitalized.
單詞首字母大寫模式
*/
INITIAL_CAPS_WORD,
/**
* This flag is a hint to the implementation that during text editing,
* the initial letter of each sentence should be capitalized.
單句首字母大寫模式
*/
INITIAL_CAPS_SENTENCE,
/**
* Capitalize all characters automatically.
全大寫模式
*/
INITIAL_CAPS_ALL_CHARACTERS,
};
*/
m_editBox->setReturnType(ui::EditBox::KeyboardReturnType::DONE);
//回車鍵形式
/*值即為顯示內(nèi)容,例如SEND即鍵盤回車鍵顯示為Send
enumclassKeyboardReturnType
{
DEFAULT,
DONE,
SEND,
SEARCH,
GO
};
*/
m_editBox->setPosition(Point(500.f,500.f));//位置
m_editBox->setDelegate(this);//設(shè)置回調(diào)代理
addChild(m_editBox);//添加到父節(jié)點(diǎn)