【问题标题】:How to separate Button Longpress Button press Up and Down events如何分离按钮长按按钮按下向上和向下事件
【发布时间】:2015-12-31 06:33:35
【问题描述】:

我有一个点击功能并长按同一个按钮。实现了长按事件,但是我需要分别找到按钮 UP_EVENT 和 DOWN_EVENTS。 如何使用OnLongClickListener 实现

 View.OnLongClickListener listener = new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View view) {

                return true;
            }

        };

【问题讨论】:

    标签: java android button long-press


    【解决方案1】:

    Implement a TouchListener within the onLongClickListener:

        View.OnLongClickListener listener = new View.OnLongClickListener() {
    
            @Override
            public boolean onLongClick(View view) {
                view.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                // PRESSED
                                return true; // if you want to handle the touch event
                            case MotionEvent.ACTION_UP:
                                // RELEASED
                                return true; // if you want to handle the touch event
                        }
                        return false;
                    }
                });
                return true;
            }
    
        };
    

    【讨论】:

      【解决方案2】:

      要检测ACTION_UPACTION_DOWN 事件,您需要实现OnTouchListener

      【讨论】:

        【解决方案3】:

        要分开,你可以这样做

        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            switch (ev.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
        
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    if (isOnClick) {                    
                        //TODO onClick code
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
        
                    }
                    break;
                default:
                    break;
            }
            return true;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-05-14
          • 1970-01-01
          • 1970-01-01
          • 2015-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多