• 文字输入


    文本框控件CCTextFieldTTF:

    const char* getString(void);    获取当前字符串
    bool attachWithIME();    激活输入法
    bool detachWithIME();    取消激活输入法
     
    例如:
    1 CCTextFieldTF text = CCTextFieldTTF::textFieldWithPlaceHodler("Input Your name...", "Arial", 20);
    2 text->setPosition(ccp(winSize.width/2, winSize.height/2 + 40));
    3 this->addChild(text);
    4 text->attachWithIME();
     
    在创建场景时,如果场景里面有文本框,那么会默认弹出键盘,如果想不自动弹出文本框,而是等用户轻触文本框再弹出键盘,实现方法如下:
    //在文本框上增加一个按钮即可
    1 CCMenuItem*  tapItem = CCMenuItemFont::create("       ", this, menu_selector(StartScene::textFiledPressed));
    2 tapItem->setPosition(cp(winSize.width/2, winSize.height/2 + 40));
    3 menu->addChild(tapItem, 1);
    4 //并且将显示输入法放到响应函数中
    5 void StartScene::textFieldPressed(cocos2d::CCObject *sender)
    6 {
    7 text->attachWithIME();
    8 }
     
    CCTextFieldTTF预留了一个代理CCTextFeildDelegate协议来通知相关事件,这个代理协议实际上是输入法代理协议的简化封装。
    //即将激活输入法,如果不想激活,应该返回true
    virtual bool onTextFeildAttachWithIME(CCTextFieldTTF *sender);
    //即将取消输入法,如果不想取消,应该返回true
    virtual bool onTextFieldDetachWithIME(CCTextFieldTTF *sender);
    //即将插入一段文字,如果不想插入,应该返回true
    virtual bool onTextFieldInsertText(CCTextFieldTTF *sender, const char * text, int nLen);
    //即将删除一段文字,如果不想删除,应该返回true
    virtual bool onTextFieldDeleteBackward(CCTextFieldTTF *sender, const char *delText, int nLen);
    //如果不希望绘制这个输入法,返回true
    virtual bool onDraw(CCTextFieldTTF *sender);
     
    弹出键盘后,如果文本框在场景的下方,键盘可能会挡住文本框,所以进行以下处理:
     1 bool StartScene::onTextFieldAttachWithIME(cocos2d::CCTextFieldTTF *sender)
     2 {
     3     this->setPosition(ccp(0, 100));
     4     return false;
     5 }
     6 bool StartScene::onTextFieldDetachWithIME(cocos2d::CCTextFieldTTF *sender)
     7 {
     8     this->setPosition(ccp(0, 0));
     9     return false;
    10 }
    11 //最后,设置文本框和协议响应方
    12 text->setDelegate(this);
  • 相关阅读:
    Azure 虚拟机安全加固整理
    AzureARM 使用 powershell 扩容系统磁盘大小
    Azure Linux 云主机使用Root超级用户登录
    Open edX 配置 O365 SMTP
    powershell 根据错误GUID查寻错误详情
    azure 创建redhat镜像帮助
    Azure Powershell blob中指定的vhd创建虚拟机
    Azure Powershell 获取可用镜像 PublisherName,Offer,Skus,Version
    Power BI 连接到 Azure 账单,自动生成报表,可刷新
    Azure powershell 获取 vmSize 可用列表的命令
  • 原文地址:https://www.cnblogs.com/Blogs-young-chan/p/5223488.html
Copyright © 2020-2023  润新知