2010-02-02 57 views
19

我想要的是有一個選項菜單,用戶可以選擇導航的菜單:採用Android的手勢

  1. 觸摸按鈕,然後向下按軌跡球選擇它
  2. 借鑑手勢生成器

預定義手勢目前的情況是,我創建了我與OnClickListener按鈕和手勢與GestureOverlayView。然後,我選擇開始一個新的Activity取決於是否使用按下按鈕或執行手勢。但是,當我試圖畫一個手勢時,它不會被拾起。只有按下按鈕才能識別。以下是我的代碼:

public class Menu extends Activity implements OnClickListener, OnGesturePerformedListener { 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //create TextToSpeech 
    myTTS = new TextToSpeech(this, this); 
    myTTS.setLanguage(Locale.US); 

    //create Gestures 
    mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures); 
    if (!mLibrary.load()) { 
    finish(); 
    } 

    // Set up click listeners for all the buttons. 
    View playButton = findViewById(R.id.play_button); 
    playButton.setOnClickListener(this); 
    View instructionsButton = findViewById(R.id.instructions_button); 
    instructionsButton.setOnClickListener(this); 
    View modeButton = findViewById(R.id.mode_button); 
    modeButton.setOnClickListener(this); 
    View statsButton = findViewById(R.id.stats_button); 
    statsButton.setOnClickListener(this); 
    View exitButton = findViewById(R.id.exit_button); 
    exitButton.setOnClickListener(this); 

    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures); 
    gestures.addOnGesturePerformedListener(this); 
} 

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { 
    ArrayList<Prediction> predictions = mLibrary.recognize(gesture); 

// We want at least one prediction 
if (predictions.size() > 0) { 
Prediction prediction = predictions.get(0); 
// We want at least some confidence in the result 
if (prediction.score > 1.0) { 
    // Show the gesture 
    Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show(); 
    //User drew symbol for PLAY 
    if (prediction.name.equals("Play")) { 
     myTTS.shutdown(); 
     //connect to game 
     // User drew symbol for INSTRUCTIONS 
    } else if (prediction.name.equals("Instructions")) { 
      myTTS.shutdown(); 
      startActivity(new Intent(this, Instructions.class)); 
     // User drew symbol for MODE 
    } else if (prediction.name.equals("Mode")){ 
     myTTS.shutdown(); 
     startActivity(new Intent(this, Mode.class)); 
     // User drew symbol to QUIT 
    } else { 
     finish(); 
    } 
    } 
} 
} 

@Override 
    public void onClick(View v) { 
     switch (v.getId()){ 
     case R.id.instructions_button: 
     startActivity(new Intent(this, Instructions.class)); 
     break; 
     case R.id.mode_button: 
     startActivity(new Intent(this, Mode.class)); 
     break; 
     case R.id.exit_button: 
     finish(); 
     break; 
    } 
} 

任何建議將不勝感激!

+0

是姿態工作?是否調用了'onGesturePerformed'? – Macarse 2011-03-13 22:33:44

+0

你的佈局是什麼樣子?有可能你的手勢疊加不在用戶正在繪製手勢的地方。 – 2011-03-20 17:06:50

+0

手勢在許多UI控件上似乎都不能很好地工作。我不得不恢復使用專用的「滑動我」控件,用戶在其上滑動並且它說「檢測到手勢」。它吃了房地產然而。看到我的應用程序CueBrain看看它的樣子。 – swinefeaster 2011-03-27 18:51:15

回答

2

如果我理解正確的,要執行兩點擊同一視圖操作和手勢動作,我不知道如何實現這一點,但是,我們可以有按鈕背後看不見的觀點來處理在上面的姿勢和按鈕將像往常一樣處理點擊事件。

默認計算器應用程序使用此方法的正常模式和高級模式之間切換。

Calculator Source

參考, com.android.calculator2.PanelSwitcher - 手勢事件 com.android.calculator2.EventListener - click事件

希望它幫助。