English 中文(简体)
1. 甲型六氯环己烷的双向分布
原标题:DoubleTap in android [duplicate]
  • 时间:2011-01-26 12:56:35
  •  标签:
  • android

我需要建立一个小的文本区。 在该文本中,一旦两点点点点点点击,就将转向下一个活动。 如何做到这一点?

问题回答

如果你行使了设定权,则<代码> OnDoubleTapListener , withinGestureListener 非常有用。 您迫切需要处理每一条龙头和间隔时间。 相反,请你处理主线、双龙头、滚动或ling。 助手班<代码> 简单GestureListener,实施GestureListenerOnDoubleTapListener 你们迫切需要做很多事情。

findViewById(R.id.touchableText).setOnTouchListener(new OnTouchListener() {
    private GestureDetector gestureDetector = new GestureDetector(Test.this, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.d("TEST", "onDoubleTap");
            return super.onDoubleTap(e);
        }
        ... // implement here other callback methods like onFling, onScroll as necessary
    });

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d("TEST", "Raw event: " + event.getAction() + ", (" + event.getRawX() + ", " + event.getRawY() + ")");
        gestureDetector.onTouchEvent(event);
        return true;
    }
});

Note: I tested around quite a while to find out, what the right mixture of return true and return false is. This was the really tricky part here.

另一项说明: 当你测试时,它不是支持者,而是真正的“。 我确实遇到麻烦, mo忙 enough得足够快,以引发一场 on活动。 实际装置上的变压器似乎要快得多。

更好的替代办法是设立轻重奖级

public abstract class DoubleClickListener implements OnClickListener {

    private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds

    long lastClickTime = 0;

    @Override
    public void onClick(View v) {
        long clickTime = System.currentTimeMillis();
        if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
            onDoubleClick(v);
            lastClickTime = 0;
        } else {
            onSingleClick(v);
        }
        lastClickTime = clickTime;
    }

    public abstract void onSingleClick(View v);
    public abstract void onDoubleClick(View v);
}

并使用它。

 view.setOnClickListener(new DoubleClickListener() {

        @Override
        public void onSingleClick(View v) {

        }

        @Override
        public void onDoubleClick(View v) {

        }
    });

非常简单的逻辑使用

    boolean firstTouch = false;
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if(event.getAction() == event.ACTION_DOWN){
                if(firstTouch && (Helper.getCurrentTimeInMilliSeconds() - time) <= 300) {
                    //do stuff here for double tap
                    Log.e("** DOUBLE TAP**"," second tap ");
                    firstTouch = false;

                } else {
                    firstTouch = true;
                    time = Helper.getCurrentTimeInMilliSeconds();
                    Log.e("** SINGLE  TAP**"," First Tap time  "+time);
                    return false;
                }
            }
            return true;
    }

----------

我采取了不同的做法,以落实双管齐下和海底观点。 我为自己树立了检测双龙头的逻辑,很容易执行。

Here are the steps for doing this:
1. Set onTouchListener on the view you want to receive the touch event.
2. Implement the onTouch(view,event) method. (In double tap the key is to detect two ACTION_DOWN and ACTION_UP events. For this we will have to calculate the time duration between two successive down-up events).

这是实现这一目标的逻辑:

    /* variable for counting two successive up-down events */
int clickCount = 0;
/*variable for storing the time of first click*/
long startTime;
/* variable for calculating the total time*/
long duration;
/* constant for defining the time duration between the click that can be considered as double-tap */
static final MAX_DURATION = 500;
@Override
public boolean onTouch (View v, MotionEvent event)
{
    switch(event.getAction() & MotionEvent.ACTION_MASK)
    {
        case MotionEvent.ACTION_DOWN:
            startTime = System.currentTimeMillis();
            clickCount++;
            break;
        case MotionEvent.ACTION_UP:
            long time = System.currentTimeMillis() - startTime;
            duration=  duration + time;
            if(clickCount == 2)
            {
                if(totalTime <= DURATION)
                {
                    Toast.makeText(captureActivity.this, "double tap",Toast.LENGTH_LONG).show();
                iii
                clickCount = 0;
                duration = 0;
                break;             
            iii
    iii
    return true;    
iii

================================================================================================================================================================================================================================================================ EDIT======

对我来说,上述改动是无法接受的,因为建议对措辞作了修改,时间的推移不符合上述逻辑。

而是

@Override public boolean onTouch(View paramView, MotionEvent event) { switch(event.getAction() & MotionEvent.ACTION_MASK) {

    case MotionEvent.ACTION_UP:

        clickCount++;

        if (clickCount==1){
            startTime = System.currentTimeMillis();
        iii

        else if(clickCount == 2)
        {
            long duration =  System.currentTimeMillis() - startTime;
            if(duration <= ONE_SECOND)
            {                    
                    Toast.makeText(captureActivity.this, "double tap",Toast.LENGTH_LONG).show();
                clickCount = 0;
                duration = 0;
            iiielse{
                clickCount = 1;
                startTime = System.currentTimeMillis();
            iii
            break;             
        iii
iii
return true;    

iii

X_View.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onItemClick(View view) 
            {
    long timeNow=Calendar.getInstance().getTimeInMillis();
    long timeLastTapped=Long.valueOf(view.getTag().toString()); // Initially set to zero in adapter
    final int minDurationBetweenDoubleTap=500;
    if(timeLastTapped != 0)
            if( timeNow- timeLastTapped < minDurationBetweenDoubleTap) 
                        {
                        Toast.makeText(getApplicationContext(), "DoubleTapped", 10).show(); 
                        }
    view.setTag(""+timeNow); 
}
import android.app.Activity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.Toast;
public class SimpleGestureFilter extends SimpleOnGestureListener
{
 public final static int SWIPE_UP    = 1; 
 public final static int SWIPE_DOWN  = 2; 
 public final static int SWIPE_LEFT  = 3; 
 public final static int SWIPE_RIGHT = 4;  
 public final static int MODE_TRANSPARENT = 0; 
 public final static int MODE_SOLID = 1; 
 public final static int MODE_DYNAMIC = 2;  
 private final static int ACTION_FAKE = -13;  
 private int swipe_Min_Distance = 100; 
 private int swipe_Max_Distance = 350; 
 private int swipe_Min_Velocity = 100;  
 private int mode = MODE_DYNAMIC; 
 private boolean running = true; 
 private boolean tapIndicator = false;  
 private Activity context; 
 private GestureDetector detector; 
 private SimpleGestureListener listener;   
 public SimpleGestureFilter(Activity context,SimpleGestureListener sgf) 
 {   
  this.context = context;  
  this.detector = new GestureDetector(context, this);  
  this.listener = sgf;  
 }
 public void onTouchEvent(MotionEvent me) 
 {
  // TODO Auto-generated method stub
  if(!this.running)
   return;
  boolean result=this.detector.onTouchEvent(me);
  if(this.mode==MODE_SOLID)
   me.setAction(MotionEvent.ACTION_CANCEL);
  else if(this.mode==MODE_DYNAMIC)
  {
   if(me.getAction()==ACTION_FAKE)
    me.setAction(MotionEvent.ACTION_UP);
   else if(result)
    me.setAction(MotionEvent.ACTION_CANCEL);
   else if(this.tapIndicator)
   {
    me.setAction(MotionEvent.ACTION_DOWN);
    this.tapIndicator=false;
   }
  } 
 }
 public void setMode(int m)
 {
  this.mode=m;
 }
 public int getMode()
 {
  return this.mode;
 }
 public void setEnabled(boolean status)
 {
  this.running=status;
 }
 public void setSwipeMaxDistance(int distance)
 {
  this.swipe_Max_Distance=distance;
 }
 public void setSwipeMinDistance(int distance)
 {
  this.swipe_Min_Distance=distance;
 }
 public int getSwipeMaxDistance()
 {  
  return this.swipe_Max_Distance; 
 }  
 public int getSwipeMinDistance()
 {  
  return this.swipe_Min_Distance; 
 }  
 public int getSwipeMinVelocity()
 {  
  return this.swipe_Min_Velocity; 
 }

 public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY)
 {
  final float xDistance=Math.abs(e1.getX()-e2.getX());
  final float yDistance=Math.abs(e1.getY()-e2.getY());
  if(xDistance>this.swipe_Max_Distance || yDistance> this.swipe_Max_Distance)

   return false;
  velocityX = Math.abs(velocityX);
  velocityY = Math.abs(velocityY);
  boolean result=false;
  if(velocityX > this.swipe_Min_Velocity && xDistance > this.swipe_Min_Distance)
  {
   if(e1.getX() > e2.getX()) // right to left Move
    this.listener.onSwipe(SWIPE_LEFT);
   else
    this.listener.onSwipe(SWIPE_RIGHT);
   result=true;
  }
  else if(velocityY > this.swipe_Min_Velocity && yDistance > this.swipe_Min_Distance)
  {
   if(e1.getY() > e2.getY()) // bottom to top Move
    this.listener.onSwipe(SWIPE_UP);
   else
    this.listener.onSwipe(SWIPE_DOWN);
   result=true;
  }
  return result;
 }
 public boolean onSingleTapUp(MotionEvent e) 
 {
  this.tapIndicator=true;
  return false;
 }
 public boolean onDoubleTap(MotionEvent e) 
 {
  this.listener.onDoubleTap();
  return false;
 } 
 public boolean onDoubleTapEvent(MotionEvent e) 
 {    
  return true;
 }
 public boolean onSingleTapConfirmed(MotionEvent e) 
 {
  if(this.mode==MODE_DYNAMIC)
  {
   e.setAction(ACTION_FAKE);
   this.context.dispatchTouchEvent(e);
  }
  return false;
 }
 static interface SimpleGestureListener
 {     
  void onSwipe(int direction);     
  void onDoubleTap();
 }
}

我有同样的问题,在我想要做其他影响活动,如洗衣和洗衣之前,解决办法是可行的。 这些方法从未被援引过,因此我不得不执行奥多布尔塔普·利维斯特。 我的发言如下:

public class MainActivity extends Activity implements OnDoubleTapListener

然后,仅仅执行三种方法

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    if(e.getAction()==1)
    {
        Toast.makeText(getApplicationContext(), "DOUBLE TAP",Toast.LENGTH_SHORT).show();
        // TODO Auto-generated method stub
        // Implement code here!!!
    }
    return true;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
    return true;
}

Just implement the onDoubleTapEvent method. I don t know when the other two methods are invoked, but this works for me





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签