cocos2dx一编辑框的使用CCEditBox

cocos2dx 输入框例子 cocos2dx中输入框的例子,新手可参考 立即下载

cocos2dx实现输入框,CCEditBox的简单用法,更多的设置可以查看API,看代码


#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"

class HelloWorld : public cocos2d::CCLayer,public cocos2d::extension::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();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);
    
    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);


	cocos2d::extension::CCEditBox* edit_name;

	//开始进入编辑
	virtual void editBoxEditingDidBegin(cocos2d::extension::CCEditBox* editBox);
	
	//结束编辑
	virtual void editBoxEditingDidEnd(cocos2d::extension::CCEditBox* editBox);
	
	//编辑框文本改变
	virtual void editBoxTextChanged(cocos2d::extension::CCEditBox* editBox, const std::string& text);
	
	//当触发return后的回调函数
	virtual void editBoxReturn(cocos2d::extension::CCEditBox* editBox);

};

#endif // __HELLOWORLD_SCENE_H__


#include "HelloWorldScene.h"

USING_NS_CC;
USING_NS_CC_EXT;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
    
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
    
    // position the label on the center of the screen
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - pLabel->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);

	//输入框的大小 
	CCSize edit_size=CCSizeMake(visibleSize.width-100,80);
	
	edit_name=CCEditBox::create(edit_size,CCScale9Sprite::create("green_edit.png"));
	edit_name->setPosition(ccp(visibleSize.width/2,visibleSize.height/2));
	edit_name->setPlaceHolder("test");//设置提示的文字?
	edit_name->setReturnType(KeyboardReturnType::kKeyboardReturnTypeDefault);
	/*edit_name->setInputFlag(kEditBoxInputFlagPassword);*/ //设置密码输入
	edit_name->setDelegate(this);//设置监听器
	this->addChild(edit_name);
    
    return true;
}

void HelloWorld::editBoxEditingDidBegin(cocos2d::extension::CCEditBox* editBox){

}
void HelloWorld::editBoxEditingDidEnd(cocos2d::extension::CCEditBox* editBox){

	CCLog("------gettext-------%s",editBox->getText());

}
void HelloWorld::editBoxTextChanged(cocos2d::extension::CCEditBox* editBox, const std::string& text){
	CCLog("-----text-------%s",editBox->getText());
}
void HelloWorld::editBoxReturn(cocos2d::extension::CCEditBox* editBox){

}


void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}



cocos2dx基础篇(15)——编辑框之二CCEditBox 【唠叨】 前面我们讲了精灵贴图、标签、菜单、按钮。感觉似乎少了点什么?UI控件里是不是应该还有个很重要的控件——编辑框。在手机网游中,启动游戏,过了开场动画后,基本上显示的第个界面应该就是游戏的登录界面了吧。输入用户名、密码什么的,这些都是需要借助编辑框来实现输入的。点击文本,弹出虚拟键盘,输入账号密码,点击登录。 cocos2dx引擎为我们提供了两类编辑框的... 阅读详情

相关推荐

Cocos2d-x学习笔记(11)(CCEditBox编辑框

CCEditBox编辑框类似于CCTextFieldTTF,但比CCTextFieldTTF更强大,比如密码输入形式、输入字体放大、f

yuxikuo_1的专栏 2616

cocos2dx中输入框输入限制数字

最近做斗牛的项目,需要处理个输入框只能显示纯数字的文本,用的是cocos2dx自带的EditBox。虽然设置了输入属性为cc.EDITBOX_INPUT_MODE_NUMERIC,但是弹出的框中仍然可以切换输入’,.’之类的字符。虽然可以在delegate中重写editBoxEditingDidEnd函数,但是这已经是在输入结束时进行的检测。后来看了下可以重写editBoxTextChanged

lyf348864485的专栏 4766

cocos2dx-3.3 使用编辑框(EditBox)

头文件要包含: #include "cocos-ext.h" USING_NS_CC_EXT; 类要继承于 class HelloWorld : public cocos2d::Layer ,public EditBoxDelegate 这里有个问题,我这样写之后会报错,提示:EditBoxDeletgate不是类名或者结构名,这个纠结了很久,后来在网上看到别人用ScoreView的时候也

奔跑的香蕉 1万+

Cocos2d-x中编辑框CCEditBox使用

//设置背景图片  //ps:CCScale9Sprite对象,是种CCSprite对象的变形,它的用法和CCSprite样,不同点是,CCScale9Sprite对象有个特性就是缩放贴图时可以尽量不失帧。  CCScale9Sprite *pBg = CCScale9Sprite::create("extensions/orange_edit.png");    //创建编辑框  CCEdi

yuan2053的博客 1523

cocos2dx基础篇(13) 编辑框之二CCEditBox

【3.x】 (1)去掉“CC” (2)设置虚拟键盘的编辑类型 >EditBoxInputMode变为强枚举EditBox::EditBoxInputMode // SINGLE_LINE //开启任何文本的输入键盘(不含换行) ANY ...

diaosong8043的博客 273

和屌丝起学cocos2dx-CCEditBox

声  明        本教程仅用于初学cocos2dx同学使用,内容由本人(孤狼)学习过程中笔记编写,本教程使用cocos2dx版本为2.1.4。本教程内容可以自由转载,但必须同时附带本声明,或注明出处。 gl.paea.cn版权所有。        Hello,大家好,欢迎回到“和屌丝起学cocos2dx”系列教程。上节我们说到了

gl.paea.cn 3273

cocos2dX UI控件之CCEditBox

刚刚看了看以前写的博客, 我们已经学了CCLabelTT

hushuai1992 1608

cocos2dx基础篇(11)——编辑框之二CCEditBox

【唠叨】     前面我们讲了精灵贴图、标签、菜单、按钮。感觉似乎少了点什么?UI控件里是不是应该还有个很重要的控件——编辑框。在手机网游中,启动游戏,过了开场动画后,基本上显示的第个界面应该就是游戏的登录界面了吧。输入用户名、密码什么的,这些都是需要借助编辑框来实现输入的。点击文本,弹出虚拟键盘,输入账号密码,点击登录。     cocos2dx引擎为我们提供了两类编辑框的控件:   ...

携墨的博客 893

Cocos2d-x CCEditBox & CCTextFieldTTF

下面简单记录下如何Cocos2d-x中创建输入编辑框。在引擎中为我们提供了这样两个类:CCEditBox 和 CCTextFieldTTF。 CCEditBox ①这个类文件的位置 ②这个类是继承自CCControlButton 和CCIMEDelegate。其中的CCIMEDelegate代理类中定义了四个代理方法,在使用的时候根据需要选择实现相应的委托方法,从方...

weixin_30687811的博客 136

cocos2dx3.16输入框:TextField和EditBox的使用

cocos2dx3.16引擎为我们提供了两类编辑框的控件: (1)CCTextFieldTTF(基于CCLabelTTF) self.textField = self.node_root:getChildByName("TextField") self.textField:setSingleLineEnabled(false) -- true:单行输入 false:多行输入...

标题 3058

cocos2dx[2.x](10)--编辑框之二CCEditBox

【唠叨】 前面我们讲了精灵贴图、标签、菜单、按钮。感觉似乎少了点什么?UI控件里是不是应该还有个很重要的控件——编辑框。在手机网游中,启动游戏,过了开场动画后,基本上显示的第个界面应该就是游戏的登录界面了吧。输入用户名、密码什么的,这些都是需要借助编辑框来实现输入的。点击文本,弹出虚拟键盘,输入账号密码,点击登录。 cocos2dx引擎为我们提供了两类编辑框的控件: (1)CCT...

qq_34240791的博客 232

COCOS2DX-游戏开发之二九】CCEditBox的各种问题

关于cocos2d-x 中 CCEditBox 的输入位置和IOS虚拟键盘位置不重合的bug http://www.cnblogs.com/lan0725/p/3210963.html

Teng's world 3652

cocos2dxCCEditBox

篇文章是用到CCTextFieldTTF来创建的个输入框,现在我们用CCEditBox来创建个输入框,它是在2.0版本后加入的,好了,看例子吧:   CCEditBox *m_InputBox; 定义个editbox;   void MyEditBoxLayer::initLayer() { CCSize size = CCDirector::sharedDirecto

专注,努力,勤奋 5967

COCOS2DX2.2.2 创建CCEditBox输入框架实现文本及密码输入

本文转载于:http://5.quanpao.com/?p=561 使用CCEditBox需要启用扩展库既extension ,因此需要引入这个空间名 有两种方法, using namespace extension 或 宏定义 USING_NS_CC_EXT 在2.2.2版本中,不需要自己再额外引入包及连接器。 但是都需要引入头文件 #include “coco...

dibiao4475的博客 340

cocos2d-x 输入框CCEditBox使用

特别说明: 这个版本的CCEditBox,设计有缺陷,背景图片的位置与输入区域的位置不同步,需要自己修改原来的代码,自己加上输入区域的坐标偏移量。 void CCEditBox::setPosition(const CCPoint& pos) {     CCControlButton::setPosition(pos);     if (m_pEditBoxImpl !=

liuzhimayi的专栏 9558
上一篇: cocos2dx一tab选项卡的实现
下一篇: Android开发之旅一fragment选项卡的实现
Flyjun-android
博客等级 码龄14年 73粉丝 73原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值