19

私が欲しいのは、ユーザーがメニューをナビゲートすることを選択できるオプションメニューを用意することです:

  1. ボタンをタッチしてからトラックボールを押し下げて選択する
  2. Gestures Builder から定義済みのジェスチャを描画する

現状では、 でボタンを作成し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;
  }  
}

どんな提案でも大歓迎です!

4

1 に答える 1

2

正しく理解していれば、同じビューでクリックアクションとジェスチャアクションの両方を実行する必要があります。これを実現する方法はわかりませんが、ボタンの後ろに非表示のビューを設定してジェスチャを処理し、上部のボタンでクリックを処理できます。いつものようにイベント。

デフォルトの電卓アプリケーションは、このアプローチを使用して、通常モードと詳細モードを切り替えます。

電卓ソース

com.android.calculator2.PanelSwitcher-ジェスチャーイベントの場合com.android.calculator2.EventListener-クリックイベントの場合を参照してください

それが役に立てば幸い。

于 2011-04-04T06:39:45.177 に答える