Android中的多个监听器

时间:2013-06-05 12:14:11

标签: java android eclipse event-listener

如何设置按钮以收听长按事件,其中按钮位于已使用手势检测器监听onFling事件的视图上?

我添加了代码以供参考。请解释我如何在一个活动中实现多个监听器。此外,是否可以为单个视图实现多个侦听器?其中一个是长时间的听众,而另一个则是一个狡猾的倾听者

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.knowledge);

    summary =(TextView)findViewById(R.id.textSummary);
    buckview = (View) findViewById(R.id.bucketView);
    ball = (View)findViewById(R.id.meatBall);
    gestureDetector = new GestureDetector(new MyGestureDetector());
    View mainview = (View) findViewById(R.id.mainView);

    /*
    //Set touch release listener for bucket view
    mainview.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == MotionEvent.ACTION_UP)
            {
                buckview.setVisibility(RelativeLayout.GONE);
            }
            return false;
        }
    });
    */


    // Set the touch listener for the main view to be our custom gesture listener
    mainview.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }

    });
    ball.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            buckview.setVisibility(ImageView.VISIBLE);
            return false;
        }
    });
    ball.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if(event.getAction()==MotionEvent.ACTION_UP)
            {
                buckview.setVisibility(ImageView.GONE);
                return false;
            }
            else
            return true;
        }
    });
}

class MyGestureDetector extends SimpleOnGestureListener {

    public void onShowPress(MotionEvent event){

    }
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    Intent intent = new Intent(Knowledge.this.getBaseContext(), Knowledge.class);

        /* If only horizontal allowed
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
            return false;
        }
        */
        // right to left swipe
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        startActivity(intent);
        Knowledge.this.overridePendingTransition(
        R.anim.slide_in_right,
        R.anim.slide_out_left
        ); 
        return false;// left to right swipe
        }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        startActivity(intent);
        Knowledge.this.overridePendingTransition(
        R.anim.slide_in_left, 
        R.anim.slide_out_right
        );
        return false;// top to bottom swipe
        } else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
        startActivity(intent);
        Knowledge.this.overridePendingTransition(
        R.anim.slide_in_top,
        R.anim.slide_out_bottom
        ); 
        return false;// bottom to top swipe
        }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
        startActivity(intent);
        Knowledge.this.overridePendingTransition(
        R.anim.slide_in_bottom, 
        R.anim.slide_out_top
        );
        return false;
        }else
        return true;
    }

    // It is necessary to return true from onDown for the onFling event to register
    @Override
    public boolean onDown(MotionEvent event) {
            return true;
    }


}


}

1 个答案:

答案 0 :(得分:0)

只需尝试

button.setOnLongClickListener(new OnLongClickListener() {

    public boolean onLongClick(View arg0) {
        // TODO Auto-generated method stub
        return false;
    }
})

如果您从true返回onLongClick,它将完美无缺。如果您从此处返回ture,则只会调用setOnLongClickListener。如果您返回false,则会调用属于此Listener的所有button。在您的情况下,无论您是Listenser还是setOnLongClickListener,都会调用setOnTouchListener

我希望这会对你有所帮助。