2

我正在尝试在我的偏好屏幕中添加手势,但我没有得到任何积极的结果,我的问题是我们可以向偏好屏幕添加手势吗?如果是,那么我如何使用下面的代码添加偏好的手势(不适用于偏好但它可以在两个 Activity 之间切换)

@Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub

        return gestureDetector.onTouchEvent(event);
    }

    SimpleOnGestureListener simpleOnGestureListener
    = new SimpleOnGestureListener(){


        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            String swipe = "";
            float sensitvity = 50;

            // TODO Auto-generated method stub
            if((e1.getX() - e2.getX()) > sensitvity){
                // for left
            Intent i=new Intent(getApplicationContext(),MyActivity.class);
                startActivity(i);
                finish();


            }else if((e2.getX() - e1.getX()) > sensitvity){
                //for right
            }else{
                swipe += "\n";
            }

            if((e1.getY() - e2.getY()) > sensitvity){
                //Swipe Up
            }else if((e2.getY() - e1.getY()) > sensitvity){
                //Swipe Down
            }



            return super.onFling(e1, e2, velocityX, velocityY);
        }
    };

    GestureDetector gestureDetector
    = new GestureDetector(simpleOnGestureListener);
4

1 回答 1

0

首先让你的 MainActivityimplements OnGestureListener 然后定义手势

    private GestureDetector gestureScanner;

将手势添加到您的 onCreategestureScanner = new GestureDetector(this); 中,这是手势的方法

@Override
public boolean onTouchEvent(final MotionEvent event)
{
    return gestureScanner.onTouchEvent(event);
}
public boolean onDown(final MotionEvent e)
{

    return true;
}
public boolean onFling(final MotionEvent e1, final MotionEvent e2,
        final float velocityX, final float velocityY)
{
    return true;
}
public void onLongPress(final MotionEvent e) {

}
public boolean onScroll(final MotionEvent e1, final MotionEvent e2,
        final float distanceX, final float distanceY)
{
    return true;
}
public void onShowPress(final MotionEvent e)
{

}
public boolean onSingleTapUp(final MotionEvent e)
{
    showHide(); // show hide the statusBar On Small Phones prefer to
    // keep it on Single Tab Cuz On Swipe Down is Kind laggy!!!
    // WTF !! :D
    return true;
}

现在,正如您在我onSingleTapUp(final MotionEvent e)用来显示隐藏状态栏的我所看到的那样。

您有六种不同的触摸事件方式“手势事件”选择其中一种并将您的意图放在上面,当用户“让我们说”滚动时,另一个活动将启动。

public boolean onScroll(final MotionEvent e1, final MotionEvent e2,
        final float distanceX, final float distanceY)
{
        Intent i=new Intent(getApplicationContext(),MyActivity.class);
            startActivity(i);
            finish();

    return true;
}

试一试,我希望它对你有好处。

于 2013-02-23T11:09:15.673 回答