Can we give both onTouchListener event and onClickListener on a single text view?

十年热恋 提交于 2019-12-24 10:55:26

问题


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()



来源:https://stackoverflow.com/questions/8786047/can-we-give-both-ontouchlistener-event-and-onclicklistener-on-a-single-text-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!