1

我在互联网上搜索了我的问题,但没有找到适合我的问题的好答案。

我想要什么:我想实现从右到左的滑动,然后让我从 MainActivity 转到我的 TimeSetActivity。

我的问题是什么?:我尝试编写这个想法,但是从右向左滑动后 AVD 没有进入 TimeSetActivity。即使我改变滑动值也不会。就好像我没有初始化手势一样。(代码本身没有发生错误)

我的代码:

package com.example.wirelessthermostat;



    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.GestureDetector.OnGestureListener;
    import android.view.Menu;
    import android.view.MotionEvent;
    import android.view.View;

    public class MainActivity extends Activity implements OnGestureListener {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void nextIntent(View view) {
    Intent intent = new Intent(this, TimesetActivity.class);
    startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onDown(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
     try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
                return false;
            }
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Intent intent = new Intent(this, TimesetActivity.class);
                startActivity(intent);
            } 
            // left to right swipe
            else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Intent intent = new Intent(this, TimesetActivity.class);
                startActivity(intent);
            }
        } catch (Exception e) {

        }
        return false;
}

@Override
public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub

}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}

}

4

0 回答 0