1.以下是CCEditBox的相关函数和类型说明:

/*

编辑框的一些函数

setText("字符串"); //设置文本

setFontColor(color); //设置文本颜色

setPlaceHolder("文本"); //设置预设文本

getPlaceHolder(对象); //获得预设文本

setMaxLength(对象); //设置最大长度

getMaxLength(对象); //获得最大长度

setInputMode(); //设置键盘模式

setInputFlag(); //设置文本类型

setReturnType(); //设置键盘return类型

1.setInputMode

这里cocos2dx给我们提供了很多键盘的模式,我们来一一了解下。

kEditBoxInputModeAny //文本键盘(含换行)

kEditBoxInputModeEmailAddr //邮件类键盘

kEditBoxInputModeNumeric //数字符号键盘

kEditBoxInputModePhoneNumber //电话号码键盘

kEditBoxInputModeUrl //URL键盘

kEditBoxInputModeDecimal //输入键盘(含小数点)

kEditBoxInputModeSingleLine //文本键盘(不含换行)

2.setInputFlag

这里cocos2dx给我们提供了5种文本类型。

kEditBoxInputFlagPassword //密码形式

kEditBoxInputFlagSensitive //敏感数据输入

kEditBoxInputFlagInitialCapsWord //每个单词首字符大写,并有提示

kEditBoxInputFlagInitialCapsSentence //第一句首字符大写,并有提示

kEditBoxInputFlagInitialCapsAllCharacters //自动大写

3.setReturnType

这里cocos2dx给我们提供了5种键盘返回类型。

kKeyboardReturnTypeDefault //默认类型

kKeyboardReturnTypeDone //Done字样

kKeyboardReturnTypeSend //Send字样

kKeyboardReturnTypeSearch //Search字样

kKeyboardReturnTypeGo //Go字样

*/

2.以下是我的登陆界面,作为测试使用!

bool HelloWorld::init(){

if ( !CCLayerColor::initWithColor(ccc4(255, 255, 2555, 255))){

return false;

}

CCLayerColor::initWithColor(ccc4(255, 0, 0, 255));

CCSize size=CCDirector::sharedDirector()->getWinSize();

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("market_sell.plist");

CCLabelTTF *l_username=CCLabelTTF::create("账号", "", 26);

l_username->setPosition(ccp(size.width* 0.5-200, 220));

addChild(l_username);

ed_username= createEditBox("sell_editboxBg.png", CCSizeMake(300,60), "", 20, ccc3(255, 0, 0), 5, "请输入帐号", kEditBoxInputModeSingleLine, kEditBoxInputFlagSensitive, kKeyboardReturnTypeDone);

ed_username->setText("");

ed_username->setPosition(ccp(size.width*0.5, 220));

addChild(ed_username);

ed_username->setDelegate(this);////设置代理

CCLabelTTF *l_password=CCLabelTTF::create("密码", "", 26);

l_password->setPosition(ccp(size.width* 0.5-200, 120));

addChild(l_password);

ed_password= createEditBox("sell_editboxBg.png", CCSizeMake(300,60), "", 20, ccc3(255, 0, 0), 12, "输入密码", kEditBoxInputModeSingleLine, kEditBoxInputFlagPassword, kKeyboardReturnTypeDone);

ed_password->setText("");

ed_password->setPosition(ccp(size.width* 0.5, 120));

addChild(ed_password);

ed_password->setDelegate(this);

this->setTouchEnabled(true);

return true;

}

//事件响应顺序如下:editBoxEditingDidBegin-> editBoxTextChanged-> editBoxEditingDidEnd-> editBoxReturn(注意:点击屏幕也会触发editBoxReturn)

void HelloWorld::editBoxEditingDidBegin(CCEditBox* editBox){

CCLog("**********Begin");

}

void HelloWorld::editBoxTextChanged(CCEditBox* editBox, const std::string& text){

CCLog("**********Changed");

}

void HelloWorld::editBoxEditingDidEnd(CCEditBox* editBox){

CCLog("**********ended");

}

void HelloWorld::editBoxReturn(CCEditBox* editBox){

CCLog("**********Return");

if (ed_username == editBox){

isNameNull=(strlen(ed_username->getText())==0);

if (isNameNull) {

CCMessageBox("Tip", "用户名不能为空");

}else{

ed_password->setPlaceHolder("点击输入");

ed_password->sendActionsForControlEvents(CCControlEventTouchUpInside);//需要再点击一次,才能输入

}

}else if (ed_password == editBox){

isPsdNull=(strlen(ed_password->getText())==0);

if (isPsdNull) {

CCMessageBox("Tip", "密码不能为空");

}

}

}

在代码里我使用了createEditBox这个全局方法。详细代码如下:

CCEditBox * createEditBox(const char *pFileName,CCSize size,const char* pFontName, int fontSize,const ccColor3B& color,int maxLength,const char* pPlaceHolderText,EditBoxInputMode inputMode,EditBoxInputFlag inputFlag,KeyboardReturnType returnType){

CCScale9Sprite *scale9Sprite=CCScale9Sprite::createWithSpriteFrameName(pFileName);

CCEditBox *editbox=CCEditBox::create(size, scale9Sprite);

editbox->setFontColor(color);

editbox->setFont(pFontName, fontSize);

editbox->setInputFlag(inputFlag);

editbox->setPlaceHolder(pPlaceHolderText);//当编辑框中没有任何字符的提示

editbox->setInputMode(inputMode);//设置输入模式

editbox->setReturnType(returnType);////设置键盘缩回按钮为return类型

editbox->setMaxLength(maxLength);//最大输入文本长度

return editbox;

}

CCEditBox用法的更多相关文章

  1. cocos2dx注册场景 使用CCEditBox实现输入框

    我们在开始玩一个游戏时,通常要做的第一件事就是注册账号,下面就让我们来制作一个简单的注册场景,我所使用的cocos2dx版本为2.2.2 在这个场景中最主要的元素就是输入框和按钮,我从网上找了一些素材 ...

  2. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  3. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  4. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  5. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  6. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  7. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  8. 【JavaScript】innerHTML、innerText和outerHTML的用法区别

    用法: <div id="test">   <span style="color:red">test1</span> tes ...

  9. chattr用法

    [root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...

随机推荐

  1. sublime 编译程序出错控制台打印PATH的解决办法

    找到sublime的安装目录 搜索 exec.py 打开找到这几句话193行左右或者搜索关键词path if "PATH" in merged_env: self.debug_te ...

  2. Java多线程初学者指南(9):为什么要进行数据同步

    Java中的变量分为两类:局部变量和类变量.局部变量是指在方法内定义的变量,如在run方法中定义的变量.对于这些变量来说,并不存在线程之间共享的问题.因此,它们不需要进行数据同步.类变量是在类中定义的 ...

  3. ANDROID_MARS学习笔记_S02_012_ANIMATION_利用AnimationListener在动画结束时删除或添加组件

    一.代码 1.xml(1)activity_main.xml <?xml version="1.0" encoding="utf-8"?> < ...

  4. Ext.onReady(function(){} )函数的作用域分析(1)

    Ext.onReady(function(){ var genResultDelete = function(){ alert('delete') ; } var renderResult = fun ...

  5. [PeterDLax著泛函分析习题参考解答]第4章 Hahn-Bananch 定理的应用

    1. 证明: 若在 4.1 节中取 $S=\sed{\mbox{正整数}}$, $Y$ 是收敛数列构成的空间, $\ell$ 由 (14) 式定义, 则由 (4) 给出的 $p$ 和由 (11) 定义 ...

  6. 经典算法面试题目-翻转一个C风格的字符串(1.2)

    题目: Write code to reverse a C-Style String. (C-String means that "abcd" is represented as ...

  7. Ext.Net中的Task控件的使用

    在用到Ext.Net中的Task控件的时候,写了一下基本的使用方法: 控件是在TaskManager里面的Tasks下面的Task 此控件的常用属性有,TaskID.Interval(设置间隔时间). ...

  8. [NOIP2007]奖学金

      题目来源:http://www.luogu.org/problem/show?pid=1093# 2007年NOIP全国联赛普及组 [题目描述 Description] 某小学最近得到了一笔赞助, ...

  9. MVC Model Binder 规则

    http://www.cnblogs.com/mszhangxuefei/archive/2012/05/15/mvcnotes_30.html 使用默认的Model Binder(Using the ...

  10. GCC内联汇编入门

    原文为GCC-Inline-Assembly-HOWTO,在google上可以找到原文,欢迎指出翻译错误. 中文版说明 由于译者水平有限,故译文出错之处,还请见谅.C语言的关键字不译,一些单词或词组( ...