2014-02-24 103 views
1

按照StopWatch GDK示例中的指導原則和約定,我無法打開MenuOption。爲Google Glass創建選項菜單

我的應用程序編譯,並能輸出「@@@@ TEST」的」嵌入式日誌語句,但似乎沒有OptionsMenu。

https://developers.google.com/glass/develop/gdk/ui/immersion-menus

這是菜單的Android的API中的方法。

openOptionsMenu(); 

我立足的代碼關閉秒錶的慣例:

/* 
* 
* Menu Code 
*/ 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
} 

@Override 
public boolean onKeyDown(int keycode, KeyEvent event) { 
    if (keycode == KeyEvent.KEYCODE_DPAD_CENTER) { 
     openOptionsMenu(); 
     Log.v("@@@@","TEST"); 
     return true; 
    } 
    return false; 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu, menu); 
    return true; 
} 
@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    // Implement if needed 
    return false; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection. Menu items typically start another 
    // activity, start a service, or broadcast another intent. 
    switch (item.getItemId()) { 
     case R.id.stop: 
      //startActivity(new Intent(this, StopStopWatchActivity.class)); 
      Log.v("@@@@","HI"); 
       return true; 
     case R.id.read_aloud: 
      Log.v("@@@@","READ_ALOUD"); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 
    //open the optionsMenu to make sure 
@Override 
public void openOptionsMenu() { 
     super.openOptionsMenu(); 

} 

用於菜單的XML

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:id="@+id/read_aloud" 
     android:title="@string/read_aloud" 
     android:icon="@drawable/ic_launcher"/> 
    <item 
     android:id="@+id/stop" 
     android:title="@string/stop" 
     android:icon="@drawable/ic_launcher"/> 
</menu> 

回答

3

編輯:我發現了我的問題。我評論說準備好的選項返回了錯誤狀態,這導致我的選擇不參與。

在這裏引用是一個很好的例子來正確地做到這一點。

感謝@ w9jds的幫助。

我無法找到問題在我的代碼中,但這裏有一個正確的菜單創建和一個水龍頭的工作示例解決方案。

https://github.com/w9jds/GlassMenuExample

什麼程序做是顯示出玻璃的應用程序,使一個Hello World卡。在點擊卡上創建一個具有「分享」選項的選項菜單。

此功能與卡片在時間線上的工作方式很相似。

菜單 - main.xml中

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item 
     android:id="@+id/share_menu_item" 
     android:title="@string/share_label" 
     android:icon="@drawable/ic_share_50"/> 

</menu> 

主要活動

package com.example.glassmenuexample; 

import com.google.android.glass.app.Card; 
import com.google.android.glass.media.Sounds; 
import com.google.android.glass.touchpad.Gesture; 
import com.google.android.glass.touchpad.GestureDetector; 

import android.media.AudioManager; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.MotionEvent; 

public class MainActivity extends Activity 
{ 
    private GestureDetector mGestureDetector; 

    private AudioManager maManager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     //initialize the audio manager 
     maManager = (AudioManager) getSystemService(this.AUDIO_SERVICE); 
     //create gesture listener 
     mGestureDetector = createGestureDetector(this); 

     //create a new card for the view 
     Card cView = new Card(this); 
     //set the text of the card to the hello world string 
     cView.setText(R.string.hello_world); 
     //set the card as the content view 
     setContentView(cView.toView()); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 


    private GestureDetector createGestureDetector(Context context) 
    { 
     GestureDetector gdDetector = new GestureDetector(context); 
     //Create a base listener for generic gestures 
     gdDetector.setBaseListener(new GestureDetector.BaseListener() 
     { 
      @Override 
      public boolean onGesture(Gesture gesture) 
      { 
       if (gesture == Gesture.TAP) 
       { 
        //play the tap sound 
        maManager.playSoundEffect(Sounds.TAP); 
        //open the menu 
        openOptionsMenu(); 
        return true; 
       } 

       return false; 
      } 
     }); 

     return gdDetector; 
    } 

    @Override 
    public boolean onGenericMotionEvent(MotionEvent event) 
    { 
     if (mGestureDetector != null) 
      return mGestureDetector.onMotionEvent(event); 

     return false; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     // Handle item selection. Menu items typically start another 
     // activity, start a service, or broadcast another intent. 
     switch (item.getItemId()) 
     { 
      case R.id.share_menu_item: 
       //do something on menu item click 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

}