COCOS2DX's WebView (Android return key handling problem)

Source: Internet
Author: User

Recently the game has access to a server platform, because there is no SDK, so payment related operations need to be done on the web side, that is, click on the top-up need to pop up a page inside the game, and locate the platform recharge address. After reviewing the relevant information, we decided to use COCOS2DX's own webview to complete the task. WebView is very simple to use, the code is as follows:

" ui/cocosgui.h "
using namespace Cocos2d::experimental::ui; M_webview = webview::create ();//M_webview is a member variable M_webview->setposition (VISIBLESIZE/2
M_webview-Setcontentsize (visiblesize);

M_webview->loadurl ("http://www.baidu.com");

M_webview->setscalespagetofit (true);

M_webview->setonshouldstartloading ([] (WebView *sender,ConstSTD::string&URL)
{
return true;}); M_webview->setondidfinishloading ([] (WebView *sender,ConstSTD::string&URL)
{}); M_webview->setondidfailloading ([] (WebView *sender,ConstSTD::string&URL)
{
}); This->addchild (M_webview);

Isn't it simple? But there was a problem with the test. After the page pops up, if you click the Back button, the game exits, and after analysis, the default action of the view processing return key is to destroy the current activity. The ideal effect is after the popup webview, if you click the Back button, first determine whether the page can be returned, if you can return to the previous page, if not return (on the original page), close WebView, back to the game.

First, the game internal return key is monitored and the return key logic is processed:

BOOLFirstscene::init () {//Register Capture ListenerAuto Listenerkeypad =eventlistenerkeyboard::create (); Listenerkeypad->onkeyreleased = Cc_callback_2 (firstscene::onkeyreleased, This); _eventdispatcher->addeventlistenerwithscenegraphpriority (Listenerkeypad, This);} voidFirstscene::onkeyreleased (Eventkeyboard::keycode keycode, Cocos2d::event *Event){    if(KeyCode = =eventkeyboard::keycode::key_backspace) {            if(M_webview)//if WebView is showing        {            if(M_webview->cangoback ())//determine if you can go back to the previous level page{M_webview-GoBack (); }            Else{M_webview->runaction (Removeself::create ());//Close WebViewM_webview=nullptr; }        }        Else //If you are in the main game screen{director::getinstance ()->popscene ();//End Game        }      }}

Next let WebView bounce out when click the Back button also execute the above logic, this to modify the WebView source code, open Cocos2dxwebview.java, the path is: src/org/cocos2dx/lib/ Cocos2dxwebview.java

  PublicCocos2dxwebview (Context context,intViewtag)    {Super (context);  This. Mviewtag =Viewtag;  This. Mjsscheme ="";  This. setfocusable (true);  This. Setfocusableintouchmode (true);  This. GetSettings (). Setsupportzoom (false);  This. GetSettings (). setdomstorageenabled (true);  This. GetSettings (). setjavascriptenabled (true); //' Searchboxjavabridge_ ' has big security risk.http://jvn.jp/en/jp/JVN53768697    Try{Method Method= This. GetClass (). GetMethod ("Removejavascriptinterface",NewClass[]{string.class}); Method.invoke ( This,"Searchboxjavabridge_"); } Catch(Exception e) {log.d (TAG,"This API level does not support ' Removejavascriptinterface '"); }     This. Setwebviewclient (Newcocos2dxwebviewclient ());  This. Setwebchromeclient (Newwebchromeclient ()); This.setonkeylistener (New Onkeylistener () {@Override public boolean OnKey (Dialoginterface dialog, I NT KeyCode, KeyEvent event) {if (keycode = = keyevent.keycode_back)//return key {C Ocos2dxglsurfaceview.getinstance (). OnKeyDown (KeyCode, keyevent)//Call the event listener function of the game main interface return true; Intercept events, prohibit events from passing down} return false; Other events do not handle, continue to pass        }    }); }

The Cocos2dxglsurfaceview is the game interface view, when the WebView return key event triggered, directly invoke the game main interface of the event listener function, and prohibit the event down pass.

COCOS2DX's WebView (Android return key handling problem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.