यह दो-उंगली डबल क्लिक का पता लगाने के लिए बनाया गया एक डबल-क्लिक श्रोता है।
चर का प्रयोग किया:
private GestureDetector gesture;
private View.OnTouchListener gestureListener;
boolean click1 = false;
boolean click2 = false;
long first = 0;
long second = 0;
गतिविधि के onCreate()
रजिस्टर करने के लिए स्पर्श इवेंट में:
gesture = new GestureDetector(getApplicationContext(), new SimpleOnGestureListener(){
public boolean onDown(MotionEvent event) {
return true;
}
});
gestureListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gesture.onTouchEvent(event);
}
};
onCreate()
गतिविधि के अंदर के बाहर:
@Override
public boolean onTouchEvent(MotionEvent event) {
try {
int action = event.getAction() & MotionEvent.ACTION_MASK;
//capture the event when the user lifts their fingers, not on the down press
//to make sure they're not long pressing
if (action == MotionEvent.ACTION_POINTER_UP) {
//timer to get difference between clicks
Calendar now = Calendar.getInstance();
//detect number of fingers, change to 1 for a single-finger double-click, 3 for a triple-finger double-click, etc.
if (event.getPointerCount() == 2) {
if (!click1) {
//if this is the first click, then there hasn't been a second
//click yet, also record the time
click1 = true;
click2 = false;
first = now.getTimeInMillis();
} else if (click1) {
//if this is the second click, record its time
click2 = true;
second = now.getTimeInMillis();
//if the difference between the 2 clicks is less than 500 ms (1/2 second)
//Math.abs() is used because you need to be able to detect any sequence of clicks, rather than just in pairs of two
//(e.g. click1 could be registered as a second click if the difference between click1 and click2 > 500 but
//click2 and the next click1 is < 500)
if (Math.abs(second-first) < 500) {
//do something!!!!!!
} else if (Math.abs(second-first) >= 500) {
//reset to handle more clicks
click1 = false;
click2 = false;
}
}
}
}
} catch (Exception e){
}
return true;
}
यह उत्तर कमाल है! – dowi
मैंने इस कोड को आजमाया है (धन्यवाद!) और यह काम नहीं किया - मुझे केवल ACTION_DOWN घटनाएं मिल रही थीं। मेरा मानना है कि onTouchEvent() फ़ंक्शन को सत्य (ACTION_DOWN के लिए) वापस करना होगा या अन्यथा ACTION_UP बाद में प्राप्त नहीं होगा। इसका संदर्भ लें: http://stackoverflow.com/a/16495363/1277048 – FuzzyAmi