如何在Android中检测左侧或右侧的滑动?
我在android中有一个edittext视图。 在此我想要检测左右滑动。 我可以通过下面的代码在空白处获得它。 但是当我在edittext上滑动时,这不起作用。 我该怎么做? 请让我知道如果我做错了什么。 谢谢。
使用的代码
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
oldTouchValue = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP:
{
float currentX = touchevent.getX();
if (oldTouchValue < currentX)
{
// swiped left
}
if (oldTouchValue > currentX )
{
swiped right
}
break;
}
}
最简单的从左到右滑动检测器:
在您的活动课中添加以下属性:
private float x1,x2;
static final int MIN_DISTANCE = 150;
并重写onTouchEvent()
方法:
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE)
{
Toast.makeText(this, "left2right swipe", Toast.LENGTH_SHORT).show ();
}
else
{
// consider as something else - a screen tap for example
}
break;
}
return super.onTouchEvent(event);
}
我喜欢@ user2999943的代码。 但为了我自己的目的只做了一些小改动。
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE)
{
// Left to Right swipe action
if (x2 > x1)
{
Toast.makeText(this, "Left to Right swipe [Next]", Toast.LENGTH_SHORT).show ();
}
// Right to left swipe action
else
{
Toast.makeText(this, "Right to Left swipe [Previous]", Toast.LENGTH_SHORT).show ();
}
}
else
{
// consider as something else - a screen tap for example
}
break;
}
return super.onTouchEvent(event);
}
这是我使用的一个可爱的类(在我想要捕获View上的事件的情况下,如果它是ViewGroup,我使用第二个实现):
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class SwipeDetector implements View.OnTouchListener{
private int min_distance = 100;
private float downX, downY, upX, upY;
private View v;
private onSwipeEvent swipeEventListener;
public SwipeDetector(View v){
this.v=v;
v.setOnTouchListener(this);
}
public void setOnSwipeListener(onSwipeEvent listener)
{
try{
swipeEventListener=listener;
}
catch(ClassCastException e)
{
Log.e("ClassCastException","please pass SwipeDetector.onSwipeEvent Interface instance",e);
}
}
public void onRightToLeftSwipe(){
if(swipeEventListener!=null)
swipeEventListener.SwipeEventDetected(v,SwipeTypeEnum.RIGHT_TO_LEFT);
else
Log.e("SwipeDetector error","please pass SwipeDetector.onSwipeEvent Interface instance");
}
public void onLeftToRightSwipe(){
if(swipeEventListener!=null)
swipeEventListener.SwipeEventDetected(v,SwipeTypeEnum.LEFT_TO_RIGHT);
else
Log.e("SwipeDetector error","please pass SwipeDetector.onSwipeEvent Interface instance");
}
public void onTopToBottomSwipe(){
if(swipeEventListener!=null)
swipeEventListener.SwipeEventDetected(v,SwipeTypeEnum.TOP_TO_BOTTOM);
else
Log.e("SwipeDetector error","please pass SwipeDetector.onSwipeEvent Interface instance");
}
public void onBottomToTopSwipe(){
if(swipeEventListener!=null)
swipeEventListener.SwipeEventDetected(v,SwipeTypeEnum.BOTTOM_TO_TOP);
else
Log.e("SwipeDetector error","please pass SwipeDetector.onSwipeEvent Interface instance");
}
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
//HORIZONTAL SCROLL
if(Math.abs(deltaX) > Math.abs(deltaY))
{
if(Math.abs(deltaX) > min_distance){
// left or right
if(deltaX < 0)
{
this.onLeftToRightSwipe();
return true;
}
if(deltaX > 0) {
this.onRightToLeftSwipe();
return true;
}
}
else {
//not long enough swipe...
return false;
}
}
//VERTICAL SCROLL
else
{
if(Math.abs(deltaY) > min_distance){
// top or down
if(deltaY < 0)
{ this.onTopToBottomSwipe();
return true;
}
if(deltaY > 0)
{ this.onBottomToTopSwipe();
return true;
}
}
else {
//not long enough swipe...
return false;
}
}
return true;
}
}
return false;
}
public interface onSwipeEvent
{
public void SwipeEventDetected(View v, SwipeTypeEnum SwipeType);
}
public SwipeDetector setMinDistanceInPixels(int min_distance)
{
this.min_distance=min_distance;
return this;
}
public enum SwipeTypeEnum
{
RIGHT_TO_LEFT,LEFT_TO_RIGHT,TOP_TO_BOTTOM,BOTTOM_TO_TOP
}
}
这是一个使用示例:
filters_container=(RelativeLayout)root.findViewById(R.id.filters_container);
new SwipeDetector(filters_container).setOnSwipeListener(new SwipeDetector.onSwipeEvent() {
@Override
public void SwipeEventDetected(View v, SwipeDetector.SwipeTypeEnum swipeType) {
if(swipeType==SwipeDetector.SwipeTypeEnum.LEFT_TO_RIGHT)
getActivity().onBackPressed();
}
});
在某些情况下,您希望检测容器上的轻扫手势并将触摸事件传递给子项,以便在这种情况下您可以创建自定义视图组,可以说RelativeLayout并覆盖onInterceptTouchEvent,并且您可以检测轻扫事件而不会阻止Touch Event传递给您的子视图,例如:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
public class SwipeDetectRelativeLayout extends RelativeLayout {
private float x1,x2;
static final int MIN_DISTANCE=150;
private onSwipeEventDetected mSwipeDetectedListener;
public SwipeDetectRelativeLayout(Context context) {
super(context);
}
public SwipeDetectRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SwipeDetectRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch(ev.getAction())
{
case MotionEvent.ACTION_DOWN:
x1 = ev.getX();
break;
case MotionEvent.ACTION_UP:
x2 = ev.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE)
{
//swiping right to left
if(deltaX<0)
{
if(mSwipeDetectedListener!=null)
mSwipeDetectedListener.swipeEventDetected();
}
}
break;
}
return super.onInterceptTouchEvent(ev);
}
public interface onSwipeEventDetected
{
public void swipeEventDetected();
}
public void registerToSwipeEvents(onSwipeEventDetected listener)
{
this.mSwipeDetectedListener=listener;
}
}
链接地址: http://www.djcxy.com/p/42597.html