Cocos2d-x Jni C + + Java intermodulation Security Refreshing UI Controls simplest example (bottom)

Source: Internet
Author: User
Tags stub

Turn from: http://blog.csdn.net/liuyuyefz/article/details/17758809

C + + Invoke Java Chapter:


C + + calls java. Refresh the Android Edit view Control



Step one: Set the framelayout as a global variable in the Cocosdxactivity class in the engine library


Step Two: Create a layout file for a text edit box


The reference code is as follows:

<?xmlversion= "1.0" encoding= "Utf-8"?>

<edittextxmlns:android= "Http://schemas.android.com/apk/res/android"

Android:layout_width= "100dip"

android:layout_height= "60dip"

android:layout_marginleft= "20DP"

android:layout_margintop= "200DP"

android:layout_marginright= ' 20DP '

Android:layout_marginbottom= ' 20DP '

>

</EditText>

Step three: Declare a global edit box in Testjni

The reference code is as follows:

public static EditText MyView;

Step Fourth: Put the text edit box just stated into the layoutinflater and add it to the Cocos2d-x framelayout


The reference code is as follows:

Layoutinflater Inflater = (layoutinflater) getsystemservice (Context.layout_inflater_service);

MyView = (edittext) layoutinflater.from (this). Inflate (R.layout.myedit,framelayout,false);

Myview.addtextchangedlistener (watcher);

Myview.setmovementmethod (Scrollingmovementmethod.getinstance ());

Framelayout.addview (MyView);



Fifth step: Add a listener to the text box and call this callback function when the text box state changes



The reference code is as follows:

Textwatcher watcher = new Textwatcher ()

{

@Override

public void aftertextchanged (Editable s)

{

TODO auto-generated Method Stub

LOG.D ("TAG", "111111");

}

@Override

public void beforetextchanged (charsequence s, int start, int count, int)

{

TODO auto-generated Method Stub

LOG.D ("TAG", "22222");

}

@Override

public void ontextchanged (charsequence s, int start, int before, int count)

{

LOG.D ("TAG", "33==[textwatcher][ontextchanged]" +s);

}

};

Step Sixth: Create a message-passing function that creates a message-received handle. Used to send C + + message, back to the main thread to notify the UI refresh (directly on other threads refreshing the main thread UI will crash)


The reference code is as follows:

public static void Hideadview (string[] str)

{

Mhandler.obtainmessage (str). Sendtotarget ();

}

private static Handlermhandler =new Handler ()

{

public void Handlemessage (msg)

{

Switch (msg.what)

{

Case 100://UPDATE_UI:

{

String[] Arrstrings = (string[]) msg.obj;

if (arrstrings[0].equals ("I ' m a Titile"))

{

TestJni.this.myView.setText ("C + + change Java editview string test!");

}

}

Break

Default

Break

}

}

};



Add: Here are two alternate functions that change the state of the input box

Myview.setbackgroundcolor (color.transparent)//change background color transparent

Framelayout.removeview (MyView);//Remove control




C + + Partial code

Step one: Create JNI for sending information


The reference code is as follows: (I will explain this code in detail below)

#if (Cc_target_platform = = cc_platform_android)

#include "platform/android/jni/jnihelper.h"///JNI Help header file to function correctly

void Helloworld::gojava ()

{

Jnimethodinfo JMI;

if (Jnihelper::getstaticmethodinfo (JMI, "Org/cocos2dx/testjni/testjni", "Hideadview", "([ljava/lang/string;) V"))

{

Jclass str_cls = Jmi.env->findclass ("java/lang/string");


Jstring str1 = Jmi.env->newstringutf ("I ' m a Titile");

Jstring str2 = Jmi.env->newstringutf ("Are yor exit game?");


Jobjectarray Arrs = Jmi.env->newobjectarray (2, str_cls, 0);

Jmi.env->setobjectarrayelement (arrs, 0, str1);

Jmi.env->setobjectarrayelement (Arrs, 1, str2);

Jmi.env->callstaticvoidmethod (Jmi.classid, Jmi.methodid, Arrs);

}

}

#endif


The first part:

#if (Cc_target_platform = = cc_platform_android)

#endif


This is a limited platform-wide macro statement, because we don't want to have code that calls Android in iOS code, so we put our code on the Android platform and it doesn't affect other platforms.


Part II:

The following code explains:

Jnimethodinfo JMI;

if (Jnihelper::getstaticmethodinfo (JMI, "Org/cocos2dx/testjni/testjni", "Hideadview", "([ljava/lang/string;) V")) 1, the above code is universal, most of the JNI format is so

Jnimethodinfo says a method is declared,


2, below Jnihelper::getstaticmethodinfo We will assign this method to the "Hideview" method in Java that we want to execute


3, the "Org/cocos2dx/testjni/testjni" part of the law is

"Package name org/cocos2dx/testjni/the class name of the class where the method is to be executed Testjni"

4, "Hideadview" for the Java side inherited the method.

5, "([ljava/lang/string;) V")]

Specify the type of return value


6,

Jclass str_cls = Jmi.env->findclass ("java/lang/string");

String Tool Library to import JNI


7, create two JNI string

Jstring str1 = Jmi.env->newstringutf ("I ' m a Titile");

Jstring str2 = Jmi.env->newstringutf ("Are yor exit game?");

8, initialize the creation of an array

Jobjectarray Arrs = Jmi.env->newobjectarray (2, str_cls, 0);

9, add two sring to an array

Jmi.env->setobjectarrayelement (arrs, 0, str1);

Jmi.env->setobjectarrayelement (Arrs, 1, str2);

10, invoke the Java method and pass the array as a parameter to the past

Jmi.env->callstaticvoidmethod (Jmi.classid, Jmi.methodid, Arrs);




The Jni method is invoked at the end of the HelloWorld program we have established to be executed anywhere.

#if (Cc_target_platform = = cc_platform_android)

Gojava ();

#endif


Then compiled C + + and Android can be true machine debugging see the results.

Related Article

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.