如果我的视图中有两个按钮,并且我想同时为这两个按钮启用OntouchListener,我的意思是触摸第一个按钮启动一个任务并按住第一个按钮触摸第二个按钮启动另一个任务。怎么做?等待帮助。
答案 0 :(得分:6)
您可以将onTouchListener添加到该视图中,该视图将处理多次触摸。
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//first finger went down
break;
case MotionEvent.ACTION_MOVE:
//a touch placement has changed
break;
case MotionEvent.ACTION_UP:
//first finger went up
break;
case MotionEvent.ACTION_CANCEL:
//gesture aborted (I think this means the finger was dragged outside of the touchscreen)
break;
case MotionEvent.ACTION_POINTER_DOWN:
//second finger (or third, or more) went down.
break;
case MotionEvent.ACTION_POINTER_UP:
//second finger (or more) went up.
break;
default: break;
}
return true;
}
检查您的按钮是否在相应的情况下被点击。
答案 1 :(得分:3)
尝试以下代码段。
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//first finger went down
break;
case MotionEvent.ACTION_MOVE:
//a touch placement has changed
break;
case MotionEvent.ACTION_UP:
//first finger went up
break;
case MotionEvent.ACTION_CANCEL:
//gesture aborted (I think this means the finger was dragged outside of the touchscreen)
break;
case MotionEvent.ACTION_POINTER_DOWN:
if(mFirstButton.isPressed() && mSecondButton.isPressed())
{// your code.}
break;
答案 2 :(得分:2)
所以我不确定你想要完成什么,但你有类似的东西:
private boolean mFirstButtonDown = false;
@Override
public boolean onTouchEvent(MotionEvent event){
// first button onTouchEvent
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mFirstButtonDown = true;
break;
case MotionEvent.ACTION_UP:
mFirstButtonDown = false;
break;
}
....
}
对于第二个按钮,只需检查它是否仍然有效:
@Override
public boolean onTouchEvent(MotionEvent event){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if (mFirstButtonDown && (event.getPointerCount() > 1)) {
// do task
}
}
要同时触摸2个按钮使用多指针(或多点触控),您可以在此帖子中找到更多详细信息: Multiple button presses for Android 2.x
另一篇可以帮助您理解多指针的好文章是Android开发人员: http://android-developers.blogspot.ro/2010/06/making-sense-of-multitouch.html
上面的代码没有经过测试,它应该只是给你一个想法:)
答案 3 :(得分:0)
您可以在两个按钮中添加两个单独的OnTouchListener
,并在Activity
中指示两个布线按钮的触摸状态,或者您可以添加一个OnTouchListener
到您的父View
,并将MotionEvent
中指针的触摸坐标与两个按钮的坐标进行比较。
答案 4 :(得分:0)
很简单
@覆盖
public boolean onTouch(View v,MotionEvent arg1)
{
switch(v.getId()){
案例R.id.button1:
如果(arg1.getAction()== MotionEvent.ACTION_DOWN)
{
//你的活动
}
打破;
案例R.id.button2:
如果(arg1.getAction()== MotionEvent.ACTION_DOWN)
{
//你的活动
}
打破;
默认:
打破;
}
返回true;
}