zoukankan      html  css  js  c++  java
  • 文字输入

    文本框控件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);
  • 相关阅读:
    LVS与Nginx区别
    Vue报错:Vue TypeError:Cannot read property ‘xxx‘ of null
    可以这样去理解group by和聚合函数
    52条SQL语句性能优化策略
    阿里云ECS搭建Typecho博客
    Java01——入门
    一天学习一个设计模式之外观模式
    一天学习一个设计模式之组合模式
    一天学习一个设计模式之桥接模式
    一天学习一个设计模式之适配器模式
  • 原文地址:https://www.cnblogs.com/Blogs-young-chan/p/5223488.html
Copyright © 2011-2022 走看看