[英]How to Add OnTouchListener and OnClickListener at a time in Linear Layout?
How can I add a event OnTouchListener
and OnclickListener
at a time in LinearLayout
? 如何在
LinearLayout
一次添加事件OnTouchListener
和OnclickListener
?
Here is my code but doesn't work 这是我的代码,但不起作用
final LinearLayout llTimeTable=(LinearLayout) findViewById(R.id.llSehriIftar);
llTimeTable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Ramadandate.class);
startActivity(intent);
}
});
llTimeTable.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
llTimeTable.setBackgroundColor(Color.rgb(51, 51, 255));
break;
case MotionEvent.ACTION_UP:
// set color back to default
llTimeTable.setBackgroundColor(Color.rgb(76, 106, 225));
break;
}
return true;
}
});
But when I only use OnclickListener
it works, and when I use only onTouch
method it works, but both at the same time doesn't work. 但是,当我只使用
OnclickListener
它可以工作,当我只使用onTouch
方法时,它可以工作,但两者同时不起作用。
Since Touch Event is more general, it is called first, then the onClick
gets fired, however, since your onTouch returns true, the event is consumed and onClick
is never reached. 由于Touch事件更为通用,因此首先调用它,然后
onClick
被触发,但是,因为onTouch返回true,所以事件被消耗并且永远不会到达onClick
。
Simply change onTouch
to return false
and your onClick will be called. 只需将
onTouch
更改为return false
,即可调用onClick。
llTimeTable.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//Your code.......
return false;
}
});
Make sure you return false in the TouchListener
becuase if you return true then that event will not be passed to other listeners. 如果返回true,请确保在
TouchListener
返回false,然后该事件将不会传递给其他侦听器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.