Can we give both onTouchListener event and onClick

2019-08-12 23:44发布

问题:

Can we give both onTouchListener event and onClickListener on a single text view...if yes can I have sample code for it.. Thanks Ali


Yes thank you friends..it works!!! But there is a small issue I am using OnClick for for moving text up and dowm and OnCreateContextMenuListener for showing menu list...Problem here is if I am using OnCreateContextMenuListener for textview1 then onclick is not performing on the Textview1...Why I dont know....I need your suggestion ..thank you –

回答1:

Here you are:

TextView tv = (TextView) getActivity().findViewById(R.id.textview_example);
                tv.setOnClickListener( new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        //YOUR CODE HERE
                    }
                });

                tv.setOnTouchListener( new OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        // TODO Auto-generated method stub
                        //YOUR CODE HERE
                        return false;
                    }
                } );

You have to remember that maybe a TouchEvent will be also fired when you receive a ClickEvent.

UPDATE:

I think that everything will be much more clear if you take a look at the Input Events documentation.



回答2:

In addition to the above answer, i would like to add that onTouchlistener will be activated onKeyDown() initially and will keep on firing whenever view is touched

and onClickListener will be fired onKeyUp()