同时使用onTouch和onCreateContextMenu

Mau*_*lin 11 android

我希望捕获用户触摸事件的坐标(为此我实现了该onTouch方法),但我还想在用户触摸屏幕时显示上下文菜单.

当使用onTouchonCreateContextMenu方法时,每个触摸事件都被发送到该onTouch方法.onCreateContextMenu永远不会调用该方法.我想这是预料之中的.然后我试图通过调用手动显示上下文菜单openContextMenu(v)onTouch方法,此方法有效,但菜单不从屏幕上消失后onContextItemSelected(MenuItem item)调用.那我怎么能做这个呢?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.record_match);
    myCourtView = new MyImageView(getApplicationContext());
    ll = (LinearLayout)findViewById(R.id.linearLayout);
    ll.addView(myCourtView, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    registerForContextMenu(myCourtView);
    myCourtView.requestFocus();
    myCourtView.setOnTouchListener(this);

}// End OnCreate


    // Implement the OnClickListener callback
   public boolean onTouch(View v, MotionEvent event) {
      //do something when user interacts with the court view
      myCourtView.processEvent(event);
      openContextMenu(v);
      myCourtView.invalidate();
      return true;
    }//End OnClickListener

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  super.onCreateContextMenu(menu, v, menuInfo);
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.shot_entry_menu, menu);
}//End onCreateContextMenu

@Override
public boolean onContextItemSelected(MenuItem item) {
  AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  switch (item.getItemId()) {
  case R.id.player_1:
    //do something
    return true;
  case R.id.player_2:
    //do something else
    return true;
  }
  return true;
}//End onContextItemSelected
Run Code Online (Sandbox Code Playgroud)

Dhe*_*ngh 0

你应该在 onTouch 中返回 false:

如果侦听器已使用该事件,则为 true,否则为 false。