使用COCOS2D-X JSB开发,在js页面中设置iOS键盘模式

 

XYSDK.h

void setKeyboardType(int type);

 

 

XYSDK.cpp

voidXYSDK::setKeyboardType(int type)

{

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    CCEGLView * pGlView = CCDirector::sharedDirector()->getOpenGLView();

    if (pGlView)

    {

        if (0==type) {

            pGlView->setIMEKeyboardDefault();

        }elseif (1 ==type) {

           pGlView->setIMEKeyboardNumber();

        }

       pGlView->setIMEKeyboardState(true);

    }

#endif

}

CCEGLView

voidCCEGLView::setIMEKeyboardNumber()

{

   

    EAGLView * view = [EAGLViewsharedEGLView];

    view.keyboardType = UIKeyboardTypeNumbersAndPunctuation;

   

}

 

voidCCEGLView::setIMEKeyboardDefault()

{

   

    EAGLView * view = [EAGLViewsharedEGLView];

   

    view.keyboardType =UIKeyboardTypeDefault;

   

}

 

 

Jsb_xy.hpp

JSBool js_xy_XYSDK_setKeyboardType(JSContext *cx, uint32_t argc, jsval *vp);

 

Jsb_xy.cpp

JSBooljs_xy_XYSDK_setKeyboardType(JSContext *cx, uint32_t argc, jsval *vp)

{

    jsval *argv = JS_ARGV(cx, vp);

    JSBool ok = JS_TRUE;

    JSObject *obj = JS_THIS_OBJECT(cx, vp);

    js_proxy_t *proxy = jsb_get_js_proxy(obj);

    XYSDK* cobj = (XYSDK *)(proxy ?proxy->ptr : NULL);

    JSB_PRECONDITION2( cobj, cx, JS_FALSE, "InvalidNative Object");

    if (argc == 1) {

        int arg0;

        ok &= jsval_to_int32(cx, argv[0], (int32_t *)&arg0);

        JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");

        cobj->setKeyboardType(arg0);

        JS_SET_RVAL(cx, vp, JSVAL_VOID);

        returnJS_TRUE;

    }

   

    JS_ReportError(cx, "wrong number of arguments: %d, was expecting%d", argc, 1);

    returnJS_FALSE;

}

使用

js文件里,attachWithIME之前设置setKeyboardType

 

setKeyboardType(0) //默认键盘

setKeyboardType(1//数字键盘

 

 

xy.XYSDK.getInstance().setKeyboardType(1);

       this._MainUI.getWidgetByName("TextField_shu_ru").attachWithIME();

原文地址:https://www.cnblogs.com/mfrbuaa/p/3850208.html