我正在制作一个类似于 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;
}
}