当前位置: 移动技术网 > IT编程>移动开发>Android > 详谈Android中onTouch与onClick事件的关系(必看)

详谈Android中onTouch与onClick事件的关系(必看)

2019年07月24日  | 移动技术网IT编程  | 我要评论

厦门到福州动车组时刻表,诡爱qvod,99bt核工厂

这几天遇到点关于android的触摸事件相关的,还跟onclick有关,暂且记下:

linearlayout分别设置了ontouchlistener,onclicklistener,onlongclicklistener及ontouchevent回调

1、在屏幕上触摸之后基本的执行流程如下:

ontouch,action=0
ontouchevent,action=0
ontouch,action=2
ontouchevent,action=2
ontouch,action=2
ontouchevent,action=2
ontouch,action=1
ontouchevent,action=1
onclick

也就是先说ontouchlistener是最先被触发的,然后是本身的ontouchevent回调;当最后的up事件发生并被ontouchevent处理后才会触发onclicklistener。

2、把ontouchevent回调中的super.ontouchevent去除,直接返回true;则流程如下:

ontouch,action=0
ontouchevent,action=0
ontouch,action=2
ontouchevent,action=2
ontouch,action=2
ontouchevent,action=2
ontouch,action=1
ontouchevent,action=1

可以看到onclicklistener永远无法被触发,也侧面说明了对onclick的触发是在ontouchevent回调中来实现的。

3、长按屏幕,流程如下:

ontouch,action=0
ontouchevent,action=0
ontouch,action=2
ontouchevent,action=2
ontouch,action=2
ontouchevent,action=2
onlongclick
ontouch,action=2
ontouchevent,action=2
ontouch,action=2
ontouchevent,action=2
ontouch,action=1
ontouchevent,action=1
onclick

当长按的时候,无需到up就会触发onlongclick的响应,但之后也会继续触发onclick的响应。

4、但如果在onlongclick中返回true,则流程如下,即只在中间触发了onlongclick,之后会继续响应touch,但当up的时候就不会再触发onclick

ontouch,action=0

ontouchevent,action=0
ontouch,action=2
ontouchevent,action=2
ontouch,action=2
ontouchevent,action=2
onlongclick
ontouch,action=2
ontouchevent,action=2
ontouch,action=2
ontouchevent,action=2
ontouch,action=1
ontouchevent,action=1

测试代码如下:

public class mainactivity extends activity {
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    framelayout frame = (framelayout)findviewbyid(r.id.container);
    linearlayouttest line = new linearlayouttest(this);
    line.setonclicklistener(new clicklistener());
    line.setonlongclicklistener(new longclicklistener());
    line.setontouchlistener(new touchlistener());
    line.setlongclickable(true);
    frame.addview(line);
  }
  public class clicklistener implements onclicklistener {
    @override
    public void onclick(view v) {
      log.e("test","onclick");
    }
  }
  public class longclicklistener implements onlongclicklistener{
    @override
    public boolean onlongclick(view v) {
      log.e("test","onlongclick");
      return true;
    }
  }
  public class touchlistener implements ontouchlistener{
    @override
    public boolean ontouch(view v, motionevent event) {
      log.e("test","ontouch,action="+event.getaction());
      return false;
    } 
  }    
}

public class linearlayouttest extends linearlayout{
  public linearlayouttest(context context) {
    super(context);
  }
  @override
  public boolean ontouchevent(motionevent event) {
    log.e("test","ontouchevent,action="+event.getaction());
    return super.ontouchevent(event);
  }
}

以上这篇详谈android中ontouch与onclick事件的关系(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网