我有以下代码,只需在按钮上设置onTouchListener
GestureDetector
。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
final GestureDetector gd = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return true;
}
});
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean res = gd.onTouchEvent(event);
Log.i("res", "res = " + res);
return res;
}
});
}
}
但是,日志显示res
值始终为false且永远不为true,这意味着我的onSingleTapConfirmed
回调永远不会将其返回值传递回onTouch()
方法。有什么方法可以在onSingleTapConfirmed
方法中获得onTouch
的返回值吗?