加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x 添加启动数字输入法的功能

发布时间:2020-12-14 20:21:09 所属栏目:百科 来源:网络整理
导读:1、EAGLView.h里面添加键盘类型属性 @property(nonatomic) UIKeyboardType keyboardType; 2、CCEGLView_ios.mm 里面添加对键盘设置的方法 //设置为只有数字输入法的键盘void CCEGLView::setIMEKeyboardNumber() { EAGLView * view = [EAGLViewsharedEGLView]

1、EAGLView.h里面添加键盘类型属性

@property(nonatomic) UIKeyboardType keyboardType;
2、CCEGLView_ios.mm 里面添加对键盘设置的方法

//设置为只有数字输入法的键盘
void CCEGLView::setIMEKeyboardNumber() 
{
    EAGLView * view = [EAGLViewsharedEGLView];
    view.keyboardType =UIKeyboardTypeNumberPad;  
}
//设置为默认的输入法键盘
void CCEGLView::setIMEKeyboardDefault() 
{
    EAGLView * view = [EAGLViewsharedEGLView];
    view.keyboardType =UIKeyboardTypePhonePad;   
}

3、CCTextFieldTTF.h里面添加自定义的输入法键盘种类来做管理

enum eKeyBoardType{
    KEY_BOARD_TYPE_NORMAL = 0,KEY_BOARD_TYPE_NUMBER,};
    inline void setKeyboardType (eKeyBoardType type) {m_keyboardType = type; }
    inline int getKeyboardType () {returnm_keyboardType; }
eKeyBoardType m_keyboardType;

4、 bool CCTextFieldTTF ::attachWithIME()改成这样:

bool CCTextFieldTTF::attachWithIME()
{
    bool bRet = CCIMEDelegate::attachWithIME();
    if (bRet)
    {
        // open keyboard
        CCEGLView * pGlView = CCDirector::sharedDirector()->getOpenGLView();
        if (pGlView)
        {
            if (getKeyboardType() ==KEY_BOARD_TYPE_NORMAL) {
                pGlView->setIMEKeyboardDefault();
            }elseif (getKeyboardType() ==KEY_BOARD_TYPE_NUMBER) {
                pGlView->setIMEKeyboardNumber();
            }
            pGlView->setIMEKeyboardState(true);
        }
    }
    return bRet;
}
5、初始化用来输入的CCTextFieldTTF的时候调用

setKeyboardType(KEY_BOARD_TYPE_NUMBER);//来设置输入法为数字即可
6. 在EAGLView中实现UITextInputTraits ,即

-(UIKeyboardType) keyboardType
{
    return keyboardType_;
}
-(void) setKeyboardType:(UIKeyboardType)keyboardType
{
    keyboardType_ = keyboardType;
}
//并在EAGLView.h添加属性
UIKeyboardType          keyboardType_;
我得补充:如果在 EAGLView中没有添加@property(nonatomic)UIKeyboardTypekeyboardType;的话,程序会在

view.keyboardType = UIKeyboardTypeASCIICapable 的时候崩溃,报错:unrecognized selector sent to instance。

7.只有调用setKeyboardType 即可实现指定的键盘类型

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读