爲cocos2d-x 添加啓動數字輸入法的功能

有文章http://blog.csdn.net/smilelance/article/details/7244759給出了正確的方向,不過照之實現,卻沒有實現相應的效果,可能是作者說的不明顯,也可以是我理解的太淺,這裏作一說明:

原文:

1、EAGLView.h裏面添加鍵盤類型屬性

@property(nonatomicUIKeyboardType 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_;


7.只有調用setKeyboardType 即可實現指定的鍵盤類型

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章