gpt4 book ai didi

android - 了解用于滑动的 OnGestureListener

转载 作者:行者123 更新时间:2023-11-29 21:31:37 25 4
gpt4 key购买 nike

我不明白为什么这段代码不显示任何 Toast。我已经实现了 OnGestureListener 接口(interface)并尝试在用户时显示一些 toast 在 Activity 上滑动。这意味着必须调用 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;
}

}

最佳答案

A GestureDetector is an Android class that can take motion events, do some mathematical magic to determine what they are, and then delegate calls to a GestureListener object as specific gesture or other motion callbacks. The GestureListener object, a class we implement, receives these calls for specific gestures that the GestureDetector recognizes and allows us to react to them as we see fit (in this case, to move a graphic around within our PlayAreaView). Although the GestureDetector handles the detection of certain motions, it doesn’t do anything specific with them nor does it handle all types of gestures.

尝试如下添加 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

关于android - 了解用于滑动的 OnGestureListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19334122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com