gpt4 book ai didi

java - 为什么手势监听器不起作用?

转载 作者:行者123 更新时间:2023-12-01 21:08:31 25 4
gpt4 key购买 nike

我想使用手势监听器,所以我编写了一个简单的代码来测试它,但它不起作用。我看了 YouTube 教程并复制了它。我没有对此进行测试,但它与我想要实现的目标非常相似。这是我第一次使用手势。这就是我:

MyClassActivity.java

public class MyClassActivity extends Activity implments GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{

private GestureDetector gestureDetector;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

this.gestureDetector = new GestureDetector(this, this);
gestureDetector.setOnDoubleTapListener(this);

textView = (TextView) findViewById(R.id.textView);

} //end of Oncreate

@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent e) {

textView.setText("onDown");
return false;
}

@Override
public void onShowPress(MotionEvent e) {
textView.setText("onShowPress");
}

@Override
public boolean onSingleTapUp(MotionEvent e) {

textView.setText("onSingleTap");
return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

textView.setText("onScroll");
return false;
}

@Override
public void onLongPress(MotionEvent e) {
textView.setText("onLongPress");
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}

@Override
public boolean onDoubleTap(MotionEvent e) {
return false;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}

最佳答案

在 onCreate 方法末尾尝试此操作。

View v = //Get your view
v.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e){
return gestureDetector.onTouchEvent(e);
}
});

关于java - 为什么手势监听器不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41733912/

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