Android Studio无法识别开关案例值

时间:2014-11-16 06:28:44

标签: android

@Override
public boolean onTouchEvent(MotionEvent event){

    int action = event.getActionMasked();
    switch (action)
    {
        case (MotionEvent.ACTION_DOWN) :
            return true;
        case (MotionEvent.ACTION_MOVE) :
            return true;

        default :
            return super.onTouchEvent(event);

    int THIS_CODE_WORKS = event.getActionMasked();
    String s = "";
    switch (THIS_CODE_WORKS)
    {
        case (MotionEvent.ACTION_DOWN) :
            s="down";
            Log.v("Action",s);
            break;
        case (MotionEvent.ACTION_MOVE) :

即使调试器显示操作变量= 2,也会忽略MotionEvent.ACTION_MOVE(2)的case语句。始终执行默认语句。这是来自Google的示例代码,但它不起作用。我无法弄清楚为什么语句没有按预期进行评估。

1 个答案:

答案 0 :(得分:0)

尝试使用getAction()并更改您的开关案例

public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();

    switch (action) {
        case MotionEvent.ACTION_DOWN: 
            // finger touches the screen
            break;

        case MotionEvent.ACTION_MOVE:
            // finger moves on the screen
            break;

        case MotionEvent.ACTION_UP:   
            // finger leaves the screen
            break;
    }

    //handled the event and no further processing is required
    return true; 
}