瀏覽代碼

Add Menu item for display of Soft Keyboard in Android Examples.
Useful when running tests that include using a key to initiate some action.

iwgeric 9 年之前
父節點
當前提交
b7b1947486

+ 55 - 1
jme3-android-examples/src/main/java/org/jmonkeyengine/jme3androidexamples/TestActivity.java

@@ -4,15 +4,21 @@ import android.app.FragmentTransaction;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.util.Log;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+import com.jme3.system.JmeSystem;
 
 public class TestActivity extends AppCompatActivity {
+    JmeFragment fragment;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_test);
 
-        JmeFragment fragment = new JmeFragment();
+        fragment = new JmeFragment();
+
         // Supply index input as an argument.
         Bundle args = new Bundle();
 
@@ -55,4 +61,52 @@ public class TestActivity extends AppCompatActivity {
         transaction.commit();
 
     }
+
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        MenuInflater inflater = getMenuInflater();
+        inflater.inflate(R.menu.test_menu_items, menu);
+
+        return true;
+    }
+
+    @Override
+    public boolean onPrepareOptionsMenu (Menu menu) {
+        MenuItem item;
+
+        item = menu.findItem(R.id.optionToggleKeyboard);
+        if (item != null) {
+//            Log.d(this.getClass().getSimpleName(), "Found ToggleKeyboard menu item");
+        }
+
+        return true;
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        switch (item.getItemId()) {
+            case R.id.optionToggleKeyboard:
+                toggleKeyboard(true);
+//                Log.d(this.getClass().getSimpleName(), "showing soft keyboard");
+                break;
+            default:
+                return super.onOptionsItemSelected(item);
+        }
+
+        return true;
+
+    }
+
+    private void toggleKeyboard(final boolean show) {
+        fragment.getView().getHandler().post(new Runnable() {
+
+            @Override
+            public void run() {
+                JmeSystem.showSoftKeyboard(show);
+            }
+        });
+
+    }
+
+
 }

+ 19 - 0
jme3-android-examples/src/main/res/menu/test_menu_items.xml

@@ -0,0 +1,19 @@
+<menu xmlns:android="http://schemas.android.com/apk/res/android"
+      xmlns:app="http://schemas.android.com/apk/res-auto"
+      xmlns:tools="http://schemas.android.com/tools"
+      tools:context="org.jmonkeyengine.jme3androidexamples.MainActivity">
+
+    <item
+        android:id="@+id/optionToggleKeyboard"
+        android:orderInCategory="100"
+        android:title="@string/strOptionShowKeyboard"
+        app:showAsAction="never"
+        android:icon="@drawable/keyboard"
+    />
+
+    <!--
+                android:icon="@mipmap/redmonkey"
+                android:icon="@mipmap/greenmonkey"
+                android:icon="@mipmap/bluemonkey"
+        -->
+</menu>

+ 5 - 0
jme3-android-examples/src/main/res/values/strings.xml

@@ -18,4 +18,9 @@
     <string name="strOptionDisableKeyEventsTitle">Disable Key Events</string>
     <string name="strOptionEnableVerboseLoggingTitle">Enable Verbose Logging</string>
     <string name="strOptionDisableVerboseLoggingTitle">Disable Verbose Logging</string>
+
+    <!-- TestActivity Menu Labels -->
+    <string name="strOptionShowKeyboard">Show Keyboard</string>
+    <string name="strOptionHideKeyboard">Show Keyboard</string>
+
 </resources>