我无法理解为什么此代码不显示任何Toast。 我已经实现了OnGestureListener接口并尝试在用户时显示一些toast 滑动活动。 这意味着必须调用onFling方法,但我没有收到任何吐司。 请帮我理解这个问题。
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;
}
}
答案 0 :(得分: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