问题描述
我有我自己的切换按钮搭配:
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;
}
我想,如果用户在我的切换按钮保存他的手指探测和改变它。但是,如果用户presses手指上的控制外,我不能检测onTouchEvent。
有什么解决方法?
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.What are the solutions?
推荐答案
您可以注册 OnTouchListener
为您的视图:
You can register OnTouchListener
for your view:
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;
}
};
通过什么 onTouch返回(查看视图,MotionEvent motionEvent)
方法定义任何其他OnTouchListener()必须审核该事件或no.You可以看到this问题。
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.
修改:结果
创建两个
OnTouchListener()类和登记每一个来观察它的动作事件。(一个为你的按钮
,一个用于这是父
)返回真正
在 onTouch
类的方法巫婆审核您的按钮移动事件,所以只有这个类的审核按钮的动作事件,而不是父母的倾听者。
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.
这篇关于onTouchEvent,检测ACTION_MOVE如果用户放下手指控制之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!