【问题标题】:android onTouchEvent "Click and Hold"android onTouchEvent“点击并按住”
【发布时间】:2020-01-13 06:00:08
【问题描述】:

我试图让一个对象响应点击保持。我可以通过按屏幕数百次来使其工作。我放了日志行来测试它。每次点击我只会得到一个日志条目,但我想要重复条目直到我放手。有一个问题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" );
        }
}

【问题讨论】:

  • 我没有清楚地理解你想要达到的目标。是否要跟踪按下特定视图的时间(触摸并按住)?
  • 您是否在为 toch 事件测试向下操作和向上操作?
  • @Archie.bpgc 目标是让日志事件重复发生,直到我将手指从屏幕上移开。
  • @alireza daryani 我向上和向下都试过了,但我认为向下是正确的。

标签: java android


【解决方案1】:

您可以使用此代码:

    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 或带有处理程序的线程。

【讨论】:

  • draw 中的 setOnTouchListener 没有添加日志条目,它应该添加日志条目,直到我将手指从屏幕上移开。因为线程需要静态变量,所以使用带有处理程序的踏面会有问题。
  • 在 UP 操作中使用带有 var 的 while 循环
  • 差不多了,我可以在 TouchEvent 之外读取鼠标坐标吗?
  • 很高兴听到它;)
猜你喜欢
  • 2011-03-22
  • 2012-03-24
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多