[英]onTouchEvent, detect ACTION_MOVE if user put down finger outside of control
I have my own toggle button with: 我有自己的切换按钮,其中包括:
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (event.getY() >= getHeight()) {
if(isChecked()) {
this.setChecked(true);
this.performClick();
}
} else if (event.getY() <= 0) {
if(!isChecked()) {
this.setChecked(false);
this.performClick();
}
}
}
return true;
}
I want detect if user holds his finger across my togglebutton and change it. 我想检测用户是否在我的切换按钮上握住了手指并进行了更改。 But if user presses a finger on the outside of the control I can not detect onTouchEvent.
但是,如果用户在控件外部按下手指,则无法检测到onTouchEvent。 What are the solutions?
有什么解决方案?
You can register OnTouchListener
for your view: 您可以为视图注册
OnTouchListener
:
mOnTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
final int action = motionEvent.getAction();
boolean ret = false;
switch (action) {
case MotionEvent.ACTION_DOWN:
ret = ...;
break;
case MotionEvent.ACTION_MOVE:
ret = ...;
break;
case MotionEvent.ACTION_UP:
ret = ...;
break;
}
return ret;
}
};
What returns by onTouch(View view, MotionEvent motionEvent)
method defines that any other OnTouchListener() must audit this event or no.You can see this question . 通过
onTouch(View view, MotionEvent motionEvent)
方法返回的onTouch(View view, MotionEvent motionEvent)
定义了任何其他OnTouchListener()必须审核此事件或否。您可以看到此问题 。
Edit : 编辑 :
Create two
OnTouchListener() classes and register each one to observe it's motion events.(one for your button
and one for it's parent
).Return true
in onTouch
method of class witch audit your buttons motion events,so only this class audit button's motion events and not parents listener. 创建
two
OnTouchListener()类,并注册每个类以观察它的运动事件。(一个用于您的button
,一个用于parent
)。在onTouch
类的onTouch
方法中onTouch
true
审核按钮的运动事件,因此仅此类审核按钮的运动事件而不是父母的听众。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.