0

如果我的视图中有两个按钮,并且我想同时为这两个按钮启用 OntouchListener,我的意思是触摸第一个按钮开始一项任务,按住第一个按钮触摸第二个按钮开始另一项任务。怎么做?等待帮助。

4

5 回答 5

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;
}

检查您的按钮是否在各自的案例中被点击。

于 2012-07-24T12:10:43.690 回答
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;
于 2012-07-25T05:30:23.143 回答
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 个按钮使用多指针(或多点触摸),您可以在这篇文章中找到更多详细信息: Android 2.x 的多个按钮按下

另一篇可以帮助你理解多指针的好文章是关于 Android 开发者的: http ://android-developers.blogspot.ro/2010/06/making-sense-of-multitouch.html

上面的代码未经测试,它应该只是给你一个想法:)

于 2012-07-24T12:13:48.247 回答
0

您可以OnTouchListener向两个按钮添加两个单独的 s,并在 中用两个布尔值指示按钮的触摸状态,或者您可以向父级Activity添加一个,并将 中的指针的触摸坐标与两个 Buttons'进行比较坐标。OnTouchListenerViewMotionEvent

于 2012-07-24T12:13:25.387 回答
0

这很简单 @Override public boolean onTouch(View v, MotionEvent arg1) { switch (v.getId()) { case R.id.button1:
if(arg1.getAction()==MotionEvent.ACTION_DOWN) { //你的事件}休息;case R.id.button2: if(arg1.getAction()==MotionEvent.ACTION_DOWN) { //你的事件 } break; 默认值:中断;} 返回真;}

于 2015-04-21T17:21:26.337 回答