您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-X游戏开发之CCTextFieldTTF(虚拟键盘输入)(十)

2013-07-21 08:59 405 查看
最近需要使用到用户键盘输入数据,主要是应用于登陆界面。

在这里首先使用CCTextField来获取用户输入,这是一个扩展库文件需要引入。

S1.首先必须继承CCTextFieldDelegate,好的,下面来看下代理继承和方法:

#include "cocos-ext.h"


public cocos2d::CCTextFieldDelegate


下面主要使用的是第二个方法是detach方法,是用于屏幕缩回。

class CC_DLL CCTextFieldDelegate
{
public:
    /**
    @brief    If the sender doesn't want to attach to the IME, return true;
    */
    virtual bool onTextFieldAttachWithIME(CCTextFieldTTF * sender)
    {
        CC_UNUSED_PARAM(sender);
        return false;
    }

    /**
    @brief    If the sender doesn't want to detach from the IME, return true;
    */
    virtual bool onTextFieldDetachWithIME(CCTextFieldTTF * sender)
    {
        CC_UNUSED_PARAM(sender);
        return false;
    }

    /**
    @brief    If the sender doesn't want to insert the text, return true;
    */
    virtual bool onTextFieldInsertText(CCTextFieldTTF * sender, const char * text, int nLen)
    {
        CC_UNUSED_PARAM(sender);
        CC_UNUSED_PARAM(text);
        CC_UNUSED_PARAM(nLen);
        return false;
    }

    /**
    @brief    If the sender doesn't want to delete the delText, return true;
    */
    virtual bool onTextFieldDeleteBackward(CCTextFieldTTF * sender, const char * delText, int nLen)
    {
        CC_UNUSED_PARAM(sender);
        CC_UNUSED_PARAM(delText);
        CC_UNUSED_PARAM(nLen);
        return false;
    }

    /**
    @brief    If the sender doesn't want to draw, return true.
    */
    virtual bool onDraw(CCTextFieldTTF * sender)
    {
        CC_UNUSED_PARAM(sender);
        return false;
    }
};


S2.继承这些方法后,在.cpp文件里面写入就可以。

//2.键盘代理方法
bool HelloWorld::onTextFieldAttachWithIME(CCTextFieldTTF * pSender)
{
    CCLOG("启动输入");
    
    //将屏幕上移,避免虚拟键盘遮挡输入框
    this->setPosition(0, 300);
    
    
    return false;
    //return true;
}

bool HelloWorld::onTextFieldDetachWithIME(CCTextFieldTTF * pSender)
{
    CCLOG("关闭输入");
    
    //将屏幕缩回
    this->setPosition(ccp(0, 0));

    //得到输入内容
    const char* inputStr = pSender->getString();
    this->UserName = pSender->getString();
    CCLOG("输入内容: %s",inputStr);
    CCLog("this->UserName: %s",this->UserName.c_str());
    return false;
    //return true:不关闭
}

bool HelloWorld::onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen)
{
    CCLOG("输入字符...:%s",text);
    CCLOG("输入长度: %d",nLen);
    string temp = text;
    userNameString += temp;
    CCLog("userNameString :%s",userNameString.c_str());
    return false;
    //return true:不会输入进字符
}

bool HelloWorld::onTextFieldDeleteBackward(CCTextFieldTTF * pSender, const char * delText, int nLen)
{
    CCLOG("删除字符:%s",delText);
    CCLOG("userNameString: %d",nLen);
    return false;
    //return true:不删除
}


S3.好的,现在来看下CCTextFiedTTF的创建和使用。

CCTextFieldTTF *userName = CCTextFieldTTF::textFieldWithPlaceHolder("名称", "Arial", 34);
    userName->setPosition(ccp(winSize.width/2+111,winSize.height/3+30));
    userName->setColor(ccc3(0, 255, 0));
    userName->setDelegate(this);
    this->addChild(userName,1);


调用键盘和关闭键盘是2个方法来控制:

bool CCTextFieldTTF::attachWithIME()
{
    bool bRet = CCIMEDelegate::attachWithIME();
    if (bRet)
    {
        // open keyboard
        CCEGLView * pGlView = CCDirector::sharedDirector()->getOpenGLView();
        if (pGlView)
        {
            pGlView->setIMEKeyboardState(true);
        }
    }
    return bRet;
}

bool CCTextFieldTTF::detachWithIME()
{
    bool bRet = CCIMEDelegate::detachWithIME();
    if (bRet)
    {
        // close keyboard
        CCEGLView * pGlView = CCDirector::sharedDirector()->getOpenGLView();
        if (pGlView)
        {
            pGlView->setIMEKeyboardState(false);
        }
    }
    return bRet;
}


当然还有获取和设置输入框内容的方法:

// input text property
    // 输入文本代理
public:
    virtual void setString(const char *text);
    virtual const char* getString(void);


// input text property
void CCTextFieldTTF::setString(const char *text)
{
    static char bulletString[] = {(char)0xe2, (char)0x80, (char)0xa2, (char)0x00};
    std::string displayText;
    int length;

    CC_SAFE_DELETE(m_pInputText);

    if (text)
    {
        m_pInputText = new std::string(text);
        displayText = *m_pInputText;
        if (m_bSecureTextEntry)
        {
            displayText = "";
            length = m_pInputText->length();
            while (length)
            {
                displayText.append(bulletString);
                --length;
            }
        }
    }
    else
    {
        m_pInputText = new std::string;
    }

    // if there is no input text, display placeholder instead
    if (! m_pInputText->length())
    {
        CCLabelTTF::setString(m_pPlaceHolder->c_str());
    }
    else
    {
        CCLabelTTF::setString(displayText.c_str());
    }
    m_nCharCount = _calcCharCount(m_pInputText->c_str());
}

const char* CCTextFieldTTF::getString(void)
{
    return m_pInputText->c_str();
}


到这里,基本的CCTextFieldTTF就介绍完了。

那么还是看下效果图吧:



notice:

看了好多介绍这个控件的文章,不知道为什么不说它的缺陷。

有一点要说的是使用这个控件的过程中,充满了血泪啊。

每一个输入框使用一个menu来代替CCTouch事件,来触发输入。

但是,键盘的缩回还是一个大问题,只有return才有反应,我哦无语。

输入长度无法控制,虚拟键盘调出后,无法获取键盘外的触摸事件,再次无语。

好的,有时间介绍一个完美的输入控件,算是慰藉了受伤的心灵。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: