Solutions for handling onclick and Ontouch method conflicts in Android

Source: Internet
Author: User

Now think of the best solution, everyone has a better welcome to inform.

Problem: There is a button in a view that requires that you can move this button by tapping it, while simply clicking don't move this button to jump to the new activity.

Difficulties encountered: The movement of the button is good implementation, through the Ontouch Motionevent.action_down, Motionevent.action_move and motionevent_up can be achieved, However, if both the Ontouch and OnClick methods are implemented, conflicts can result. The theory on this aspect of the online answer is very good, below I write my solution.

The button Movement example code is as follows (transfer from Netizen):

 Public BooleanOnTouch (View V, motionevent event) {intaction=event.getaction (); LOG.I ("", "Touch:" +action); Switch(action) { CaseMotionEvent.ACTION_DOWN:lastX= (int) event.getrawx (); Lasty= (int) Event.getrawy ();  Break; /*** Layout (l,t,r,b) * L left position, relative to parent T Top position, relativ             E to parent R right position, relative to parent B Bottom position, relative to Parent * */             CaseMotionevent.action_move:intDX = (int) event.getrawx ()-Lastx; intDY = (int) Event.getrawy ()-lasty;                Move (v, dx, dy);  Break; Casemotionevent.action_up: Break; }        return false; }    Private voidMove (View V,intDxintdy) {        intleft = V.getleft () +DX; inttop = v.gettop () +dy; intright = V.getright () +DX; intBottom = V.getbottom () +dy; if(Left < 0) { left= 0; Right= left +v.getwidth (); }        if(Right >screenwidth) { Right=ScreenWidth; Left= Right-v.getwidth (); }        if(Top < 0) {Top= 0; Bottom= Top +v.getheight (); }        if(Bottom >screenheight) {Bottom=ScreenHeight; Top= Bottom-v.getheight ();        } v.layout (left, top, right, bottom); LOG.I ("", "position??" + Left + "," + Top + "," + Right + "," +bottom); }    

The core approach to solving the problem is:

Do not provide the OnClick method for this button, that is, method calls that do not add Onclicklistener,onclick are implemented in the Ontouch method.

Specific Description:

Add a Boolean global variable initialized to False (indicating that the button does not move), once the motionevent.action_move in the Ontouch is triggered, then the button is moved, the click action is not triggered, When the motionevent.action_up is triggered, no action is taken and the global variable is changed to initialize false, whereas if there is no move, then the click action is triggered.

Solutions for handling onclick and Ontouch method conflicts in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.