Cocos2d-x Jni c ++ java Intermodulation and refreshing each other UI controls the simplest example (below)

Source: Internet
Author: User

 

 

C ++ calls java:

 

C ++ calls java. Colleagues refreshed the android Edit View Control

 

 

Step 1:

Step 2: Create a layout File

Step 3:

 

Public static EditText myView;


Step 4: Add a text input box

 

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 );

 

 

Step 5: Add a listener

 

 

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 after)

{

// 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 6:

Public static void hideAdView (String [] str)

{

MHandler. obtainMessage (100, str). sendToTarget ();

}

 

Private static HandlermHandler = new Handler ()

{

Public void handleMessage (Message msg)

{

Switch (msg. what)

{

Case 100: // UPDATE_UI:

{

String [] arrStrings = (String []) msg. obj;

If (arrStrings [0]. equals (I'm a tititile ))

{

TestJni. this. myView. setText (C ++ Change Java EditView string Test !);

}

}

Break;

Default:

Break;

}

}

};

 

 

Supplement:

// MyView. setBackgroundColor (Color. TRANSPARENT); // the background Color is TRANSPARENT.

// Framelayout. removeView (myView); // remove the control

 

 

 

C ++ code

 

# If (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID)

# Include platform/android/jni/JniHelper. h

Void HelloWorld: goJava ()

{

JniMethodInfo jmi;

If (JniHelper: getStaticMethodInfo (jmi, org/cocos2dx/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


Then call

 

# If (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID)

GoJava ();

# Endif

 

Finally, compile c ++ and android


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.