如何在文本上滑动以执行某些操作

时间:2015-01-16 18:12:05

标签: android text swipe

我有一个问题: 我想在文字上从左向右滑动以拨打电话

这里是我的MainActivity:

public class MainActivity extends ActionBarActivity {

    final TextView text = (TextView)findViewById(R.id.gtext);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {

            public void onSwipeRight() {
                text.setText("right");
            }


            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        });
    }
}

OnSwipeTouchListener.java:感谢Mirek Rusin

public class OnSwipeTouchListener implements View.OnTouchListener {
    public final GestureDetector gestureDetector;

    public OnSwipeTouchListener (Context ctx){
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }
                    result = true;
                }
                else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
                result = true;

            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }

}

我在手机上进行了测试,但它的力量已经关闭。有人可以为我的问题提出建议或其他方式吗?非常感谢

2 个答案:

答案 0 :(得分:0)

public class MainActivity extends ActionBarActivity {

TextView text; 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.gtext);
text.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {

    public void onSwipeRight() {
        text.setText("right");
    }


    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
});
}
}

答案 1 :(得分:0)

您可以使用以下代码。它基于goole devTeam代码: DevBytes: Animating ListView Deletion 基本上代码显示了当用户滑动(左或右)时如何从列表视图中删除(和淡出)一行。我修改了代码,现在它没有做任何事情,但它检测到已经进行了滑动!(而不仅仅是触摸)。 那么,你可以做任何你想做的事! (代码的底部有10行)

希望它有所帮助!

@Override
    public boolean onTouch(View v, MotionEvent event) {
            if (mSwipeSlop < 0) {
                mSwipeSlop = ViewConfiguration.get(getActivity()).getScaledTouchSlop();
            }
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mAnimating) {
                    // Multi-item swipes not handled
                    return true;
                }
                mItemPressed = true;
                mDownX = event.getX();
                break;
            case MotionEvent.ACTION_CANCEL:
                mItemPressed = false;
                break;
            case MotionEvent.ACTION_MOVE: {
                if (mAnimating) {
                    return true;
                }
                float x = event.getX();
                if (isRuntimePostGingerbread()) {
                    x += v.getTranslationX();
                }
                float deltaX = x - mDownX;
                float deltaXAbs = Math.abs(deltaX);
                if (!mSwiping) {
                    if (deltaXAbs > mSwipeSlop) {
                        mSwiping = true;
                        myList.requestDisallowInterceptTouchEvent(true);
                        // mBackgroundContainer.showBackground(v.getTop(), v.getHeight());
                    }
                }
            }
                break;
            case MotionEvent.ACTION_UP: {
                if (mAnimating) {
                    return true;
                }
                // User let go - figure out whether to animate the view out,
                // or back into place
                if (mSwiping) {
                    float x = event.getX();
                    if (isRuntimePostGingerbread()) {
                        x += v.getTranslationX();
                    }
                    float deltaX = x - mDownX;
                    float deltaXAbs = Math.abs(deltaX);
                    float fractionCovered;
                    float endX;
                    final boolean remove;
                    if (deltaXAbs > v.getWidth() / 3) {
                        // Greater than a quarter of the width - animate it
                        // out
                        fractionCovered = deltaXAbs / v.getWidth();
                        endX = deltaX < 0 ? -v.getWidth() : v.getWidth();
                        remove = true;
                    } else {
                        // Not far enough - animate it back
                        fractionCovered = 1 - (deltaXAbs / v.getWidth());
                        endX = 0;
                        remove = false;
                    }
                    // Animate position and alpha
                    long duration = (int) ((1 - fractionCovered) * SWIPE_DURATION);
                    //**do whatever you want to do after user swipe**
                } else {
                    mItemPressed = false;
                }
            }
                break;
            default:
                return false;
            }
            return false;
        }