Multiple button presses for Android 2.x

爱⌒轻易说出口 提交于 2019-11-26 14:52:35

问题


I am relatively new to this still, and I have been developing a small app that would benefit greatly from a user being able to press 2 buttons at one time. What is the best method for achieving this? I dont think that an OnClickListener works like that, and I have seen examples for doing this with an OnTouch event. However, I do not know how to set up button presses with an OnTouch event.


回答1:


You would have to handle touch events yourself. With the multi-pointers (aka multi-touch) API it's very easy. Just override the onTouchEvent() method or register an OnTouchListener on your buttons.




回答2:


@Override
public boolean onTouchEvent (MotionEvent event) {
    if (event.getAction()==MotionEvent.ACTION_UP) {
        // reset all buttons
        ...
    }
    else {
        int count=event.getPointerCount(),vx1=-1,vy1=-1,vx2=-1,vy2=-1;
        if (count>=1) {
            vx1=(int)event.getX(0);
            vy1=(int)event.getY(0);
        }
        if (count>=2) {
            vx2=(int)event.getX(1);
            vy2=(int)event.getY(1);
        }
        ...
    }
    return true;
}


来源:https://stackoverflow.com/questions/2528160/multiple-button-presses-for-android-2-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!