连续的"Action_DOWN"在Android中 [英] Continuous "Action_DOWN" in Android

查看:112
本文介绍了连续的"Action_DOWN"在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Override
public boolean onTouchEvent(MotionEvent event) {

if(event.getAction()==MotionEvent.ACTION_DOWN){
Log.d(VIEW_LOG_TAG, "Touching Down");
}
    if(event.getAction()==MotionEvent.ACTION_UP){
Log.d(VIEW_LOG_TAG, "Not Touching");
}
}

我要记录->应该在记录"中连续显示"Touching Down",直到松开手指为止.

I want Log-->"Touching Down" should be displayed in Log continuously until finger is released.

请帮助

这真的很重要...没有这个,我无法在ma Project中继续进行.

Its Really Important...I cant Proceed further in ma Project without this.

推荐答案

您不能在UI线程中执行此操作.在UI-Thread中运行的代码必须简短,以保持UI响应速度.

You cannot do that in the UI-Thread. Code running in UI-Thread must be short to keep UI responsive.

因此您需要创建一个线程.

So you need to create a thread.

  • ACTION_DOWN
  • 时启动线程
  • 在线程中:用您的日志写一个循环(使用标志停止循环)
  • ACTION_UP 时:更改标志(这将导致线程结束循环.
  • start the thread when ACTION_DOWN
  • In the thread : write a loop with your log (use a flag to stop the loop)
  • when ACTION_UP : change the flag (this will cause the end of the loop in your thread.

示例代码:

AtomicBoolean actionDownFlag = new AtomicBoolean(true);

Thread loggingThread = new Thread(new Runnable(){
     public void run(){
         while(actionDownFlag.get()){
             Log.d(VIEW_LOG_TAG, "Touching Down");
             //maybe sleep some times to not polute your logcat
         }
         Log.d(VIEW_LOG_TAG, "Not Touching");
     }
});

@Override
public boolean onTouchEvent(MotionEvent event) {

    if(event.getAction()==MotionEvent.ACTION_DOWN){
        loggingThread.start();
    }
    if(event.getAction()==MotionEvent.ACTION_UP){
        actionDownFlag.set(false);
    }
}

这篇关于连续的"Action_DOWN"在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆