Android to detect when you are holding down a button

我们两清 提交于 2019-12-10 13:19:05

问题


I need to be able to tell when the user is holding a button down and when the user lets go. This is different from onClickListener and onLongClickListener. How would i go about doing something like this?

For example I press a button that starts a chronometer.(pseudo code)

if ButtonIsBeingPressed
{
chronometer start(); //and keep going
}
else chronometer stop();
//or on release or something
}

回答1:


Look into the OnTouchListener it has MotionEvents for Down (press) and Up (release):

view.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Start
            break;
        case MotionEvent.ACTION_UP:
            // End
            break;
        }
        return false;
    }
});


来源:https://stackoverflow.com/questions/11713642/android-to-detect-when-you-are-holding-down-a-button

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