cocos2d-x 为CCEditBox添加纯数字0到9的键盘输入类型并讲解其使用

感谢点评与关注,欢迎转载与分享。
勤奋努力,持之以恒!

首先,在项目中打开CCEditBox.h文件,在枚举EditBoxInputMode添加一代表纯数字的枚举类型kEditBoxInputModePadNumber代码如下

enum EditBoxInputMode
{
    /**
     * The user is allowed to enter any text, including line breaks.
     */
    kEditBoxInputModeAny = 0,
    
    /**
     * The user is allowed to enter an e-mail address.
     */
    kEditBoxInputModeEmailAddr,

    /**
     * The user is allowed to enter an integer value.
     */
    kEditBoxInputModeNumeric,

    /**
     * The user is allowed to enter a phone number.
     */
    kEditBoxInputModePhoneNumber,

    /**
     * The user is allowed to enter a URL.
     */
    kEditBoxInputModeUrl,

    /**
     * The user is allowed to enter a real number value.
     * This extends kEditBoxInputModeNumeric by allowing a decimal point.
     */
    kEditBoxInputModeDecimal,

    /**
     * The user is allowed to enter any text, except for line breaks.
     */
    kEditBoxInputModeSingleLine,
    
    //自己添加一个0到9纯数字的
    kEditBoxInputModePadNumber
};

接着打开项目中CCEditBoxImplIOS.mm文件,在方法setInputMode中把kEditBoxInputModePadNumber添加上:

void CCEditBoxImplIOS::setInputMode(EditBoxInputMode inputMode)
{
    switch (inputMode)
    {
        case kEditBoxInputModeEmailAddr:
            m_systemControl.textField.keyboardType = UIKeyboardTypeEmailAddress;
            break;
        case kEditBoxInputModeNumeric:
            m_systemControl.textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
            break;
        case kEditBoxInputModePhoneNumber:
            m_systemControl.textField.keyboardType = UIKeyboardTypePhonePad;
            break;
        case kEditBoxInputModeUrl:
            m_systemControl.textField.keyboardType = UIKeyboardTypeURL;
            break;
        case kEditBoxInputModeDecimal:
            m_systemControl.textField.keyboardType = UIKeyboardTypeDecimalPad;
            break;
        case kEditBoxInputModeSingleLine:
            m_systemControl.textField.keyboardType = UIKeyboardTypeDefault;
            break;
        case kEditBoxInputModePadNumber: //0到9纯数字键盘输入类型
            m_systemControl.textField.keyboardType = UIKeyboardTypeNumberPad;
            break;
        default:
            m_systemControl.textField.keyboardType = UIKeyboardTypeDefault;
            break;
    }
}

OK!这样就完成了。。。下面是讲解使用,简单的封装了一个方法,好方便以后使用。。讲解代码如下:

.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

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

using namespace std;
using namespace cocos2d;
using namespace extension;



class HelloWorld : public CCLayer, public CCEditBoxDelegate
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();
    
    CCEditBox* createEditBox(const CCSize& _size, const char* _imageName, const char* _fontName, int _fontSize,
                                         const ccColor3B& _fontColor, const char* _pText, const ccColor3B& _pColor, int _maxLength,
                             int _inputMode, int _inputFlag);
    
    virtual void editBoxEditingDidBegin(CCEditBox* editBox);
    virtual void editBoxEditingDidEnd(CCEditBox* editBox);
    virtual void editBoxTextChanged(CCEditBox* editBox, const std::string& text);
    virtual void editBoxReturn(CCEditBox* editBox);
    
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

.cpp文件

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    //添加一背景
    CCSprite* background = CCSprite::create("HelloWorld.png");
    background->setScale(2);
    background->setPosition(ccp(size.width*0.5, size.height*0.5));
    this->addChild(background);
    
    //名字输入框 name
    CCEditBox* name_edit = createEditBox(CCSize(size.width*0.8, 100), "orange_edit.png", "Marker Felt", 50, ccRED, "Name:", ccWHITE, 10, -1, -1);
    name_edit->setDelegate(this); //进行注册
    name_edit->setPosition(ccp(size.width*0.5, size.height*0.8));
    this->addChild(name_edit);
    
    //年龄输入框 age
    CCEditBox* age_edit = createEditBox(CCSize(size.width*0.8, 100), "green_edit.png", "Marker Felt", 50, ccGREEN, "Age:", ccWHITE, 20, 7, -1);
    age_edit->setDelegate(this);
    age_edit->setPosition(ccp(size.width*0.5, size.height*0.5));
    this->addChild(age_edit);
    
    //密码输入框 password
    CCEditBox* password_edit = createEditBox(CCSize(size.width*0.8, 100), "yellow_edit.png", "Marker Felt", 50, ccYELLOW, "Password:", ccWHITE, 20, 7, 0);
    password_edit->setDelegate(this);
    password_edit->setPosition(ccp(size.width*0.5, size.height*0.2));
    this->addChild(password_edit);
    
    return true;
}


/*
 *_size:        输入框大小(决定框的宽高)
 *_imageName:   输入框底图(可任意大小的图)
 *_fontName:    所输入文字字体
 *_fontSize:    所输入文字大小
 *_fontColor:   所输入文字颜色
 *_pText:       当编辑框中没有任何字符的提示
 *_pColor:      当编辑框中没有任何字符的提示颜色
 *_maxLength:   最大输入文本长度
 *_inputMode:   设置输入键盘模式
 *_inputFlag:   设置输入显示类型
 *
 */
CCEditBox* HelloWorld::createEditBox(const CCSize& _size, const char* _imageName, const char* _fontName, int _fontSize,
                                    const ccColor3B& _fontColor, const char* _pText, const ccColor3B& _pColor, int _maxLength,
                                    int _inputMode, int _inputFlag)
{
    CCEditBox* editBox = CCEditBox::create(_size, CCScale9Sprite::create(_imageName));
    editBox->setFontName(_fontName);
    editBox->setFontSize(_fontSize);
    editBox->setFontColor(_fontColor);
    editBox->setPlaceHolder(_pText);
    editBox->setPlaceholderFontColor(_pColor);
    editBox->setMaxLength(_maxLength);
    
    //使用默认传 -1
    switch (_inputMode) {
        case 0://开启任何文本的输入键盘,包括换行
        {
            editBox->setInputMode(kEditBoxInputModeAny);
        }
            break;
        case 1://开启 邮件地址 输入类型键盘
        {
            editBox->setInputMode(kEditBoxInputModeEmailAddr);
        }
            break;
        case 2://开启 数字符号 输入类型键盘
        {
            editBox->setInputMode(kEditBoxInputModeNumeric);
        }
            break;
        case 3://开启 电话号码 输入类型键盘
        {
            editBox->setInputMode(kEditBoxInputModePhoneNumber);
        }
            break;
        case 4://开启 URL 输入类型键盘
        {
            editBox->setInputMode(kEditBoxInputModeUrl);
        }
            break;
        case 5://开启 数字 输入类型键盘,允许小数点
        {
            editBox->setInputMode(kEditBoxInputModeDecimal);
        }
            break;
        case 6://开启任何文本的输入键盘,不包括换行
        {
            editBox->setInputMode(kEditBoxInputModeSingleLine);
        }
            break;
        case 7://0到9纯数字输入
        {
            editBox->setInputMode(kEditBoxInputModePadNumber);
        }
            break;
        default:
            break;
    }
    
    //使用默认传 -1
    switch (_inputFlag) {
        case 0://密码形式输入
        {
            editBox->setInputFlag(kEditBoxInputFlagPassword);
        }
            break;
        case 1://敏感数据输入、存储输入方案且预测自动完成
        {
            editBox->setInputFlag(kEditBoxInputFlagSensitive);
        }
            break;
        case 2://每个单词首字母大写,并且伴有提示
        {
            editBox->setInputFlag(kEditBoxInputFlagInitialCapsWord);
        }
            break;
        case 3://第一句首字母大写,并且伴有提示
        {
            editBox->setInputFlag(kEditBoxInputFlagInitialCapsSentence);
        }
            break;
        case 4://所有字符自动大写
        {
            editBox->setInputFlag(kEditBoxInputFlagInitialCapsAllCharacters);
        }
            break;
        default:
            break;
    }
    
    //设置返回类型 就是键盘右下角那个键是 return 、Done 、Send 、Search 、Go等字样
    editBox->setReturnType(kKeyboardReturnTypeDone);
    return editBox;
}

//开始编辑
void HelloWorld::editBoxEditingDidBegin(CCEditBox* editBox)
{
    CCLog("Begin");
}
//编辑框文字改变
void HelloWorld::editBoxTextChanged(CCEditBox* editBox, const std::string& text)
{
    CCLog("Changed");
}
//触发return后的回调函数
void HelloWorld::editBoxEditingDidEnd(CCEditBox* editBox)
{
    CCLog("End");
}
//结束编辑
void HelloWorld::editBoxReturn(CCEditBox* editBox)
{
    CCLog("Return");
    CCLog("输入框内容为:%s",editBox->getText());
}


效果截图:

cocos2d-x 为CCEditBox添加纯数字0到9的键盘输入类型并讲解其使用_第1张图片


cocos2d-x 为CCEditBox添加纯数字0到9的键盘输入类型并讲解其使用_第2张图片


cocos2d-x 为CCEditBox添加纯数字0到9的键盘输入类型并讲解其使用_第3张图片



你可能感兴趣的:(cocos2d-x 为CCEditBox添加纯数字0到9的键盘输入类型并讲解其使用)