cocos2dx 3.x 编辑框 输入框

#include"cocos2d.h"

#include"cocos-ext.h"

USING_NS_CC;

USING_NS_CC_EXT;

using namespace cocos2d::ui;

注:因为版本更迭,editbox原本是在cocos-ext中,现行版本则更换到了ui中,所以以上头文件和名空间根据自己的版本选择(新版本使用ext也可正常使用,原因见下图,但不建议)

cocos2dx 3.x 编辑框 输入框_第1张图片

如下,使用EditBox的类要继承ui::EditBoxDelegate

(可使用using namespace cocos2d::ui以省略ui::,本人项目因为需要使用到ext所以为了避免混淆添加了ui::)

class HomeEditBox : public Node,ui::EditBoxDelegate

在类中要声明并实现以下函数:

virtual void editBoxEditingDidBegin(ui::EditBox* editBox);

virtual void editBoxEditingDidEnd(ui::EditBox* editBox);

virtual void editBoxTextChanged(ui::EditBox* editBox,conststd::string& text);//编辑框内容改变

virtual void editBoxReturn(ui::EditBox* editBox);//点击键盘回车/确认按钮



m_editBox=ui::EditBox::create(Size(170,80),ui::Scale9Sprite::create("editBoxBg.png"));

//size为输入框大小,第二个参数为输入框背景,使用Scale9Sprite

m_editBox->setFontSize(40);//输入内容字体大小

m_editBox->setFontColor(Color3B::Black);//输入内容字体颜色

m_editBox->setPlaceHolder("Please enter a number:");//输入框提示内容,当输入内容为空时显示,如果内容不为空则不显示

m_editBox->setPlaceholderFontColor(Color3B::GRAY);//提示内容字体颜色

m_editBox->setMaxLength(5);//输入内容长度

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.数字 包含0-9 “.” delete

*/

NUMERIC,

/**

* The user is allowed to enter a phone number.手机号包含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.小数

*/

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.

密码模式,以“ * ”代替输入内容

*/

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.

敏感模式  输入内容不会存储于词典或者用于联想输入、快捷辅助输入等

*/

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);

//回车键形式

/*值即为显示内容,例如SEND即键盘回车键显示为Send

enumclassKeyboardReturnType

{

DEFAULT,

DONE,

SEND,

SEARCH,

GO

};

*/

m_editBox->setPosition(Point(500.f,500.f));//位置

m_editBox->setDelegate(this);//设置回调代理

addChild(m_editBox);//添加到父节点

你可能感兴趣的:(cocos2dx 3.x 编辑框 输入框)