1

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

1 回答 1

0

如果您false从中返回onDown(),则系统假定您要忽略手势的其余部分,并且永远不会调用 GestureDetector.OnGestureListener 的其他方法。这有可能在您的应用程序中导致意外问题。您应该返回的唯一时间falseonDown()如果您真的想忽略整个手势。

实际上,我建议您为您的按钮创建子类,您可以在其中覆盖dispatchTouchEventonTouchEvent处理您的手势。它将允许简化您的活动类和重用组件。此外,为 onTapHold、onTapRelease 等操作创建回调接口也是一个好主意。

希望你设法解决它!

于 2015-03-28T10:21:53.907 回答