About Android double-click events

Source: Internet
Author: User

Hello everyone, I believe everyone knows that there is a method in the API for the android double-click event, but it must be available in the activity.

You may have different opinions on using the double-click event API without using Android.

Use API in Activity

Advantage: saves time and effort. You can simply use things written by others without worrying about bugs.

Disadvantages:CodeWriting in the activity always feels messy.

Write an ondoubleclick method by yourself

Advantages: Self-writing, good control, strong flexibility, where to put.

Disadvantage: it takes some time to check bugs.

 

Okay. Recently, I also encountered a problem that I need to handle double-click events.

I used the android API, but I also sorted it out.

There are only two classes, of course, activity, and ondoubleclick event processing.

 

 1   Import Android. App. activity;
2 Import Android. OS. Bundle;
3 Import Android. View. gesturedetector;
4 Import Android. View. motionevent;
5 Import Android. View. View. ontouchlistener;
6
7 /**
8 *
9 * <P> class instruction: Core Activity on the Internal interface </P>
10 * Create in 2011-12-2 @ Author Mercury
11 */
12 Public Class Gameactivity Extends Activity {
13 Gameactivityhelper helper;
14 Private Gesturedetector Gd;
15 @ Override
16 Public Void Oncreate (bundle savedinstancestate ){
17 Super . Oncreate (savedinstancestate );
18 Helper = New Gameactivityhelper ( This );
19 Helper. INIT ();
20 Setcontentview (helper. getgameview ());
21 GD = New Gesturedetector (This , New Ondoubleclick ());
22 }
23 @ Override
24 Public Boolean Ontouchevent (motionevent event ){
25 Return GD. ontouchevent (event );
26 }
27 }


 

 1   Import Android. View. gesturedetector;
2 Import Android. View. motionevent;
3
4 Public Class Ondoubleclick Extends Gesturedetector. simpleongesturelistener {
5 @ Override
6 Public Boolean Ondoubletap (motionevent e ){
7 // Todo
8 Return False ;
9 }
10 // @ Override
11 // Public Boolean ondoubletapevent (motionevent e ){
12 // Return super. ondoubletapevent (E );
13 // }
14 }


The Code is also neat. In the API, double-click can be processed in two ways,

One is to double-click and execute it once: ondoubletap

One is to double-click and execute twice: ondoubletapevent

Here, I only need to double-click it and execute it once. It is commented out.

Also, you need to pay special attention to the method of overwriting the parent activity in your own activity.

@ Override
Public Boolean ontouchevent (motionevent event ){
Return GD. ontouchevent (event );
}
This method. I have seen on the Internet that many people will require you to rewrite the ontouch method after implementing implements ontouchlistener in the activity to achieve double-click.

This is completely misleading. I personally tried it. Double-clicking is not valid. I tried it in sdk1.6 for normal clicks.

This is a detail and I hope you will pay more attention to it.

Here, ontouchevent is a method in the activity, rather than implementing an interface, which is why the event can only be double-clicked in the activity,

If you need to use androidapi to process double-click events, you must override the ontouchevent method in the activity. Otherwise, all methods related to gesturedetector are invalid.

As long as ontouch implements the view or activity of the ontouchlistener interface, it cannot implement some functions of gesturedetector.

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.