我制作了一个像Snapchat相机按钮一样的按钮,可以选择单击或按住/释放。
点击 - 打印点击
按住 - 按住并打开发行版 - 发布
完美地完成工作,但ontouch永远不会接到电话。我真的需要帮助,谢谢。
这是我的代码:
public class MainActivity extends Activity implements View.OnTouchListener {
private Button TouchMe;
private GestureDetector gesture;
private boolean isSpeakButtonLongPressed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TouchMe = (Button) findViewById(R.id.button);
gesture = new GestureDetector(this,new Gesture());
TouchMe.setOnTouchListener(this);
}
class Gesture extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener{
private boolean mIsLongpressEnabled = false;
public boolean onSingleTapUp(MotionEvent ev) {
Toast.makeText(MainActivity.this, "Tap", Toast.LENGTH_SHORT).show();
return true;
}
public void onLongPress(MotionEvent ev) {
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}
@Override
public boolean onDown (MotionEvent event) {
return false;
}
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
return false; // Right to left
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
return false; // Left to right
}
if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
return false;
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
return false; // Top to bottom
}
return false;
}
public void setIsLongpressEnabled(boolean isLongpressEnabled) {
mIsLongpressEnabled = isLongpressEnabled;
}
public boolean isLongpressEnabled() {
return mIsLongpressEnabled;
}
public boolean onTouchEvent (MotionEvent ev){
if(ev.getAction() == MotionEvent.ACTION_DOWN){
handel.postDelayed(run, 2000);
return true;
}else if(ev.getAction() == MotionEvent.ACTION_UP){
handel.removeCallbacks(run);
Toast.makeText(MainActivity.this, "release!", Toast.LENGTH_LONG).show();
return true;
};
return true;
}
Handler handel = new Handler();
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
handel.postDelayed(run, 2000);
return true;
}else if(motionEvent.getAction() == MotionEvent.ACTION_UP){
handel.removeCallbacks(run);
Toast.makeText(MainActivity.this, "release!", Toast.LENGTH_LONG).show();
return true;
};
return gesture.onTouchEvent(motionEvent);
}
}
Runnable run = new Runnable() {
@Override
public void run() {
// Your code to run on long click
Toast.makeText(MainActivity.this, "hold", Toast.LENGTH_LONG).show();
}
};
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
gesture.onTouchEvent(motionEvent);
return true;
}
}
答案 0 :(得分:0)
如果从false
返回onDown()
,系统会假定您要忽略其余的手势,而GestureDetector.OnGestureListener的其他方法永远不会被调用。这可能会在您的应用中导致意外问题。您应该从false
返回onDown()
的唯一时间是您是否真的想忽略整个手势。
实际上,我建议您为按钮创建子类,您可以在其中覆盖dispatchTouchEvent
和onTouchEvent
并处理您的手势。它将允许简化您的活动类并重用组件。另外,为onTapHold,onTapRelease等操作创建回调接口也是个好主意。
希望你设法解决它!