1

我无法理解为什么这段代码没有显示任何 Toast。我已经实现了 OnGestureListener 接口并尝试在用户滑动活动时显示一些吐司。这意味着必须调用 onFling 方法,但我没有收到任何 toast。请帮我理解问题。

import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends Activity implements OnGestureListener {

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

@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 e) {
    // TODO Auto-generated method stub
    Toast.makeText(this,"onDown",Toast.LENGTH_LONG).show();
    return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
    Toast.makeText(this,"Swipe to Explore the Happiness Path",Toast.LENGTH_LONG).show();
    return false;
}

@Override
public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(this,"onLongPress",Toast.LENGTH_LONG).show();

}

@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`enter code here`
    return false;
}

}

4

1 回答 1

2

GestureDetector 是一个 Android 类,它可以接收运动事件,执行一些数学魔术来确定它们是什么,然后将调用委托给 GestureListener 对象作为特定手势或其他运动回调。GestureListener 对象是我们实现的一个类,它接收对 GestureDetector 识别的特定手势的这些调用,并允许我们在我们认为合适的时候对它们做出反应(在这种情况下,在我们的 PlayAreaView 中移动图形)。尽管 GestureDetector 处理某些动作的检测,但它不会对它们做任何特定的事情,也不会处理所有类型的手势。

尝试如下添加 GestureDetector 并实现onTouch事件来检测屏幕的触摸:

public class MainActivity extends Activity implements OnGestureListener {

private GestureDetector gDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
gDetector = new GestureDetector(this);
}
 @Override
public boolean onTouchEvent(MotionEvent me) {
    return gDetector.onTouchEvent(me);
}

@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 e) {
    // TODO Auto-generated method stub
    Toast.makeText(this,"onDown",Toast.LENGTH_LONG).show();
    return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
    Toast.makeText(this,"Swipe to Explore the Happiness Path",Toast.LENGTH_LONG).show();
    return false;
}

@Override
public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(this,"onLongPress",Toast.LENGTH_LONG).show();

}

@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`enter code here`
    return false;
}
}

了解更多GestureDetector

于 2013-10-12T12:39:44.083 回答