Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. android_app* mApplication;
  2.  
  3. ...
  4.  
  5. void displayKeyboard(bool pShow) {
  6. // Attaches the current thread to the JVM.
  7. jint lResult;
  8. jint lFlags = 0;
  9.  
  10. JavaVM* lJavaVM = mApplication->activity->vm;
  11. JNIEnv* lJNIEnv = mApplication->activity->env;
  12.  
  13. JavaVMAttachArgs lJavaVMAttachArgs;
  14. lJavaVMAttachArgs.version = JNI_VERSION_1_6;
  15. lJavaVMAttachArgs.name = "NativeThread";
  16. lJavaVMAttachArgs.group = NULL;
  17.  
  18. lResult=lJavaVM->AttachCurrentThread(&lJNIEnv, &lJavaVMAttachArgs);
  19. if (lResult == JNI_ERR) {
  20. return;
  21. }
  22.  
  23. // Retrieves NativeActivity.
  24. jobject lNativeActivity = mApplication->activity->clazz;
  25. jclass ClassNativeActivity = lJNIEnv->GetObjectClass(lNativeActivity);
  26.  
  27. // Retrieves Context.INPUT_METHOD_SERVICE.
  28. jclass ClassContext = lJNIEnv->FindClass("android/content/Context");
  29. jfieldID FieldINPUT_METHOD_SERVICE =
  30. lJNIEnv->GetStaticFieldID(ClassContext,
  31. "INPUT_METHOD_SERVICE", "Ljava/lang/String;");
  32. jobject INPUT_METHOD_SERVICE =
  33. lJNIEnv->GetStaticObjectField(ClassContext,
  34. FieldINPUT_METHOD_SERVICE);
  35. jniCheck(INPUT_METHOD_SERVICE);
  36.  
  37. // Runs getSystemService(Context.INPUT_METHOD_SERVICE).
  38. jclass ClassInputMethodManager = lJNIEnv->FindClass(
  39. "android/view/inputmethod/InputMethodManager");
  40. jmethodID MethodGetSystemService = lJNIEnv->GetMethodID(
  41. ClassNativeActivity, "getSystemService",
  42. "(Ljava/lang/String;)Ljava/lang/Object;");
  43. jobject lInputMethodManager = lJNIEnv->CallObjectMethod(
  44. lNativeActivity, MethodGetSystemService,
  45. INPUT_METHOD_SERVICE);
  46.  
  47. // Runs getWindow().getDecorView().
  48. jmethodID MethodGetWindow = lJNIEnv->GetMethodID(
  49. ClassNativeActivity, "getWindow",
  50. "()Landroid/view/Window;");
  51. jobject lWindow = lJNIEnv->CallObjectMethod(lNativeActivity,
  52. MethodGetWindow);
  53. jclass ClassWindow = lJNIEnv->FindClass(
  54. "android/view/Window");
  55. jmethodID MethodGetDecorView = lJNIEnv->GetMethodID(
  56. ClassWindow, "getDecorView", "()Landroid/view/View;");
  57. jobject lDecorView = lJNIEnv->CallObjectMethod(lWindow,
  58. MethodGetDecorView);
  59.  
  60. if (pShow) {
  61. // Runs lInputMethodManager.showSoftInput(...).
  62. jmethodID MethodShowSoftInput = lJNIEnv->GetMethodID(
  63. ClassInputMethodManager, "showSoftInput",
  64. "(Landroid/view/View;I)Z");
  65. jboolean lResult = lJNIEnv->CallBooleanMethod(
  66. lInputMethodManager, MethodShowSoftInput,
  67. lDecorView, lFlags);
  68. } else {
  69. // Runs lWindow.getViewToken()
  70. jclass ClassView = lJNIEnv->FindClass(
  71. "android/view/View");
  72. jmethodID MethodGetWindowToken = lJNIEnv->GetMethodID(
  73. ClassView, "getWindowToken", "()Landroid/os/IBinder;");
  74. jobject lBinder = lJNIEnv->CallObjectMethod(lDecorView,
  75. MethodGetWindowToken);
  76.  
  77. // lInputMethodManager.hideSoftInput(...).
  78. jmethodID MethodHideSoftInput = lJNIEnv->GetMethodID(
  79. ClassInputMethodManager, "hideSoftInputFromWindow",
  80. "(Landroid/os/IBinder;I)Z");
  81. jboolean lRes = lJNIEnv->CallBooleanMethod(
  82. lInputMethodManager, MethodHideSoftInput,
  83. lBinder, lFlags);
  84. }
  85.  
  86. // Finished with the JVM.
  87. lJavaVM->DetachCurrentThread();
  88. }
  89.  
  90. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  91. imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
  92.  
  93. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  94. imm.showSoftInput(editText.getWindowToken(), 0);
  95.  
  96. import android.view.inputmethod.InputMethodManager;
  97. import android.content.Context;
  98.  
  99. public class MyNativeActivity extends android.app.NativeActivity
  100. {
  101. public void showKeyboard()
  102. {
  103. InputMethodManager imm = ( InputMethodManager )getSystemService( Context.INPUT_METHOD_SERVICE );
  104. imm.showSoftInput( this.getWindow().getDecorView(), InputMethodManager.SHOW_FORCED );
  105. }
  106.  
  107. public void hideKeyboard()
  108. {
  109. InputMethodManager imm = ( InputMethodManager )getSystemService( Context.INPUT_METHOD_SERVICE );
  110. imm.hideSoftInputFromWindow( this.getWindow().getDecorView().getWindowToken(), 0 );
  111. }
  112. }
  113.  
  114. void DisplayKeyboard( bool bShow )
  115. {
  116. // Attaches the current thread to the JVM.
  117. JavaVM* pJavaVM = m_pNativeActivity->vm;
  118. JNIEnv* pJNIEnv = m_pNativeActivity->env;
  119.  
  120. JavaVMAttachArgs javaVMAttachArgs;
  121. javaVMAttachArgs.version = JNI_VERSION_1_6;
  122. javaVMAttachArgs.name = "NativeThread";
  123. javaVMAttachArgs.group = NULL;
  124.  
  125. jint nResult = pJavaVM->AttachCurrentThread( &pJNIEnv, &javaVMAttachArgs );
  126. if ( nResult != JNI_ERR )
  127. {
  128. // Retrieves NativeActivity.
  129. jobject nativeActivity = m_pNativeActivity->clazz;
  130. jclass ClassNativeActivity = pJNIEnv->GetObjectClass( nativeActivity );
  131.  
  132. if ( bShow )
  133. {
  134. jmethodID MethodShowKeyboard = pJNIEnv->GetMethodID( ClassNativeActivity, "showKeyboard", "()V" );
  135. pJNIEnv->CallVoidMethod( nativeActivity, MethodShowKeyboard );
  136. }
  137. else
  138. {
  139. jmethodID MethodHideKeyboard = pJNIEnv->GetMethodID( ClassNativeActivity, "hideKeyboard", "()V" );
  140. pJNIEnv->CallVoidMethod( nativeActivity, MethodHideKeyboard );
  141. }
  142.  
  143. // Finished with the JVM.
  144. pJavaVM->DetachCurrentThread();
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement