LongClick状态卡住了

时间:2012-06-12 00:57:17

标签: android

如果我'点击',它将打印“点击”。但是,一旦我'长按',它将只打印“长按”,无论长度如何。

LinearLayout ll = new LinearLayout(this);

ll.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) 
        System.out.println("click");
    }
});

ll.setOnLongClickListener(new OnLongClickListener(){
    @Override
    public boolean onLongClick(View v) {
        v.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        System.out.println("long click");                   
                    case MotionEvent.ACTION_MOVE:
                    case MotionEvent.ACTION_UP:
                }
                return false;
            }                      
        });                     
        return true;
    }
});

2 个答案:

答案 0 :(得分:0)

可能会改变:

ll.setOnLongClickListener(new OnLongClickListener(){
    @Override
        public boolean onLongClick(View v) {
        v.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            System.out.println("long click");                   
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
            }
        return false;
        }                      
        });                     
    return true;
    }
});

ll.setOnLongClickListener(new OnLongClickListener(){
    @Override
    public boolean onLongClick(View v) {
        System.out.println("long click");  
        v.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                    case MotionEvent.ACTION_UP:
                }
                return false;
            }                      
        });                     
        return true;
    }
});

这样,当它收到长按,而不是你在“设置”后触摸的事件时,它会打印“长按”

答案 1 :(得分:0)

TouchListener怎么样?它将在ACTION_DOWN上记录时间,如果ACTION_UP时间足够长,则从MotionEvent中拉出坐标。

ll.setOnTouchListener(new View.OnTouchListener() {
    Calendar down;
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            down = Calendar.getInstance();
            break;
        case MotionEvent.ACTION_UP:
            // Only continue if longer than 2 seconds
            if(Calendar.getInstance().getTimeInMillis() - down.getTimeInMillis() > 2000) {
                // Touch coordinates: event.getX(), event.getY()
            }
        }
        return true;
    }
});