我正在尝试让一个对象响应保留的单击。我可以通过按数百次屏幕来使其工作。我把日志行进行测试。我每次点击只会得到一个日志条目,但是我想重复条目直到放手为止。存在一个具有相同请求的问题here,但唯一的答案并未完全涵盖该主题,并且未解决该解决方案的所有问题。必须有一个更简单的方法。愤怒的小鸟点击并按住。我确实认为需要一个代码示例,但是如果需要的话,这里是一个。

public class GameView extends SurfaceView implements Runnable {

    private Thread thread;
    private boolean isPlaying;

    public GameView( GameActivity activity, int screenx, int screeny ) {
        super( activity );

    //tried it here
        /*setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d( "note", "hold" );
                if(event.getAction() == MotionEvent.ACTION_UP){

                    // Do what you want
                    return true;
                }
                return false;
            }
        });*/
    }

    @Override
    public void run() {
        while ( isPlaying ) {
            //do stuff
        draw();
        }
    }

    public void draw () {
        if ( getHolder().getSurface().isValid() ) {
            Canvas canvas = getHolder().lockCanvas();
        //tried it here
            setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d( "note", "hold" );
                if(event.getAction() == MotionEvent.ACTION_UP){
                    // Do what you want
                    return true;
                }
                return false;
            }
        });
            getHolder().unlockCanvasAndPost( canvas );
        }
    }

    public void resume () {
    isPlaying = true;
        thread = new Thread( this );
        thread.start();
    }

    public void pause () {
    try {
        isPlaying = false;
                thread.join();
    } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //tried it here
    Log.d( "note", "press" );
        return true;
    }
}


快到了,这就是我到目前为止所拥有的。剩下的唯一问题是,如果我单击并按住屏幕,然后再移动到另一个位置,则不会得到另一个mouseEvent。我需要event.getY()。

public boolean mouseDown = false;

public boolean onTouchEvent(MotionEvent event) {
    Log.d( "note", "press" );
    switch ( event.getAction() ) {
        case MotionEvent.ACTION_DOWN:
        Log.d( "note", "down" );
        mouseDown = true;
        mouseLooper(  );
        break;
        case MotionEvent.ACTION_UP:
        Log.d( "note", "up" );
        mouseDown = false;
        break;
    }
    return true;
}

public void mouseLooper ( ) {
        while ( mouseDown ) {
            Log.d( "note", "looping" );
        }
}


***我可以在TouchEvent之外读取鼠标位置吗?

知道了,当鼠标或手指移动了多少次时,日志条目“ press”的确出现了。您可以从mouseLooper访问当前y位置,或发送给它的其他任何位置。这是工作代码示例。

public boolean mouseDown = false;

public boolean onTouchEvent(MotionEvent event) {
    Log.d( "note", "press" );
    mouseLooper( (int) event.getY() );
    switch ( event.getAction() ) {
        case MotionEvent.ACTION_DOWN:
        Log.d( "note", "down" );
        mouseDown = true;
        mouseLooper( (int) event.getY() );
        break;
        case MotionEvent.ACTION_UP:
        Log.d( "note", "up" );
        mouseDown = false;
        break;
    }
    return true;
}

public void mouseLooper ( int currentY ) {
        while ( mouseDown ) {
            Log.d( "note", "looping" );
        }
}

最佳答案

您可以使用以下代码:

    yourBtn.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //code that you want do when pressed
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            //code when touch stoped
        }
        return false;
    }
});


您必须在代码末尾删除onTouch覆盖。

如果要重复代码,则可以使用while或带有处理程序的线程。

09-28 02:36