【问题标题】:Detect when a user moves off of a button in Android检测用户何时离开 Android 中的按钮
【发布时间】:2011-10-21 12:12:35
【问题描述】:

是否可以检测到用户在按下按钮后将手指从按钮上移开的任何时间?

我已经确定当用户垂直离开按钮时 onTouchListener 将获得一个动作(它给出一个动作:ACTION_CANCEL),但我无法找到一种方法来确定用户何时水平离开按钮.

public boolean onTouch(View arg0, MotionEvent arg1) {
    switch(arg1.getAction()){
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        switch(arg0.getId()){
        case R.id.my_button:
            button.setBackgroundResource(R.drawable.untouched);
            break;
        }
        break;
    case MotionEvent.ACTION_DOWN:
        switch(arg0.getId()){
        case R.id.my_button:
            button.setBackgroundResource(R.drawable.touched);
            break;
        }
        break;
    }
    return false;
}

【问题讨论】:

    标签: android button ontouchlistener


    【解决方案1】:

    尝试下一步:

    arg1.getX();
    arg1.getY();
    

    arg1 - 来自您的代码

    在这里您可以获取 MotionEvent 的坐标。接下来你将使用这个坐标做任何事情

    希望对你有帮助

    【讨论】:

    • 这听起来正是我想要的。我会告诉你我的测试进展如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    相关资源
    最近更新 更多