由于onDown在长按时也会执行,所以不能再onDown和长按监听器的onlongclick中分别执行各种的动作(这样长按时会执行onlongclick和ondown两个动作)
使用手势可以很好解决这个问题
package lon.detector;
import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TextView;
public class MyText extends TextView implements OnGestureListener
{
private GestureDetector detector;
public MyText(Context context)
{
super(context);
detector = new GestureDetector(this);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
detector.onTouchEvent(event);
return super.onTouchEvent(event);
}
public boolean onDown(MotionEvent e)
{
Log.e("-----", "---: on Down");
return false;
}
public void onShowPress(MotionEvent e)
{
Log.e("-----", "---: on ShowPress");
}
public boolean onSingleTapUp(MotionEvent e)
{
Log.e("-----", "---: on SingleTapUp");
return false;
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY)
{
Log.e("-----", "---: on Scroll");
return false;
}
public void onLongPress(MotionEvent e)
{
Log.e("-----", "---: on Long");
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
Log.e("-----", "---: on fling");
return false;
}
}
onLongPress里写长按事件
onSingleTapUp里写非长按的点击事件
这样就不会出现上述长按会调用两个动作的问题了