你好我是android的新手。我创建了一个服务来检测屏幕上的手势。为了获得触摸事件,我动态地创建了一个视图视图并将其添加到窗口管理器。我能够触摸,但我无法获得手势。它显示" dispatchTouchEvent"方法错误。我用活动试了一下它运行正常。这是我的代码
public class GestureActivity extends Activity implements OnGestureListener
{
TextView textview;
GestureDetector detector;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textview = new TextView(this);
textview.setText("Gesture Demo");
detector=new GestureDetector(this);
setContentView(textview);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
return detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e)
{
Toast.makeText(getApplicationContext(), "OnDown Gesture", 100).show(); return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
Toast.makeText(getApplicationContext(), "Fling Gesture", 100).show(); return true;
}
@Override
public void onLongPress(MotionEvent e)
{
Toast.makeText(getApplicationContext(), "Long Press Gesture", 100).show();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
Toast.makeText(getApplicationContext(), "Scroll Gesture", 100).show(); return false;
}
@Override
public void onShowPress(MotionEvent e)
{
Toast.makeText(getApplicationContext(), "Show Press gesture", 100).show();
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
Toast.makeText(getApplicationContext(), "Single Tap Gesture", 100).show();
return true;
}
}
如何通过动态添加视图在服务中获取此手势?