根据在线教程,我设置了一个滑动检测操作,该操作在一个项目中有效,但在另一个项目中完全无害。这是为什么?对于后者,eclipse上没有错误,在启动时,log cat上没有任何内容,也没有任何内容表明它已被选中。
xml方面只是一个简单的文本视图,但在java后端如下。
public class Swipesample extends Activity {
float x1,x2;
float y1, y2;
}
public boolean onTouchEvent(MotionEvent touchevent)
{
switch (touchevent.getAction())
{
// when user first touches the screen we get x and y coordinate
case MotionEvent.ACTION_DOWN:
{
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP:
{
x2 = touchevent.getX();
y2 = touchevent.getY();
//if left to right sweep event on screen
if (x1 < x2)
{
Toast.makeText(this, "Left to Right Swap Performed", Toast.LENGTH_LONG).show();
}
// if right to left sweep event on screen
if (x1 > x2)
{
Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show();
}
// if UP to Down sweep event on screen
if (y1 < y2)
{
Toast.makeText(this, "UP to Down Swap Performed", Toast.LENGTH_LONG).show();
}
//if Down to UP sweep event on screen
if (y1 > y2)
{
Toast.makeText(this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show();
}
break;
}
}
return false;
}
}
答案 0 :(得分:0)
返回true,如果返回false,则运动事件将不再发送到您的视图。
答案 1 :(得分:0)
我对同一个tut也有同样的问题,在通过以下方式修改后,它就像一个魅力。
public boolean onTouchEvent(MotionEvent e) {
int eventaction = e.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN: {
x1 = e.getX();
y1 = e.getY();
break;
}
case MotionEvent.ACTION_UP: {
x2 = e.getX();
y2 = e.getY();
//if left to right sweep event on screen
if (x1 > x2+300){
// Todo whatever you want to be done
}
// if right to left sweep event on screen
if (x1+300 < x2){
// Todo whatever you want to be done
}
// if UP to Down sweep event on screen
if (y1 > y2+300){
// Todo whatever you want to be done
}
//if Down to UP sweep event on screen
if (y1+300 < y2){
// Todo whatever you want to be done
}
break;
}
}
return false;
}
您可能会注意到我在x1,x2和y1,y2之间的比较中添加了300。这只是在垂直/水平滑动允许一些容差,否则我发现它太敏感。 希望它有所帮助; o)