手指离开按钮时的事件

时间:2012-09-11 11:14:33

标签: android

  

可能重复:
  onPress/onRelease in Android

当用户按住按钮时,按钮会改变颜色,当用户离开按钮时,按钮会返回到正常状态。

是否有捕获此事件的事件?

我尝试搜索像button.onStateChanged这样的东西,但没有运气..

编辑: 当用户长时间点击我想开始自动增量

// Auto increment for a long click
        increment.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View a) {
                autoIncrement = true;

                repeatUpdateHandler.removeCallbacks(mRepeatUpdateHandler);
                repeatUpdateHandler.post(mRepeatUpdateHandler);

                return false;
            }
        });

当用户用手指离开按钮时,我想停止自动增量

increment.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP && autoIncrement) {

                //Stop autoincrement

                repeatUpdateHandler.removeCallbacks(mRepeatUpdateHandler);
                autoIncrement = false;
            }

            return false;
        }
    });

这是处理程序:

RepetetiveUpdater mRepeatUpdateHandler = new RepetetiveUpdater();
 ....

class RepetetiveUpdater implements Runnable {
    public void run() {
        if (autoIncrement) {
            increment();
            repeatUpdateHandler.postDelayed(new RepetetiveUpdater(),
                    REPEAT_DELAY);
        } else if (autoDecrement) {
            decrement();
            repeatUpdateHandler.postDelayed(new RepetetiveUpdater(),
                    REPEAT_DELAY);
        }
    }
}

但这只适用于用户将手指抬到按钮上方,当他将其拖到屏幕上的其他位置时,计数器会继续运行,任何想法?

3 个答案:

答案 0 :(得分:4)

我不知道下面的代码是否完全符合您的要求。将调用increment方法,直到用户停止单击或手指不在视图的边界内。所有操作都在onTouch回调中完成:

    final Rect r = new Rect();
    // this is the platform's default timeout for recognizing a long press
    final long LONG_PRESS_TIMOUT = ViewConfiguration.getLongPressTimeout();
    increment.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final int action = event.getAction();
            // get the View's Rect relative to its parent
            v.getHitRect(r);
            // offset the touch coordinates with the values from r
            // to obtain meaningful coordinates
            final float x = event.getX() + r.left;
            final float y = event.getY() + r.top;
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // simulate a long press
                // if the user stops pressing before the long press timeout
                // expires this will be canceled
                repeatUpdateHandler.postDelayed(mRepeatUpdateHandler,
                        LONG_PRESS_TIMOUT);
                break;
            case MotionEvent.ACTION_MOVE:
                // if the touch coordinates are not in the Views' rect then
                // cancel any Runnable
                if (!r.contains((int) x, (int) y)) {
                    repeatUpdateHandler
                            .removeCallbacks(mRepeatUpdateHandler);
                }
                break;
            case MotionEvent.ACTION_UP:
                // the user raised his finger, cancel the Runnable
                repeatUpdateHandler.removeCallbacks(mRepeatUpdateHandler);
                break;
            }
            return false;
        }
    });

Runnable部分:

class RepetetiveUpdater implements Runnable {
    public void run() {
        increment();
        repeatUpdateHandler.postDelayed(mRepeatUpdateHandler, 1000);
    }
}

int i = 0;

public void increment() {
    Log.e("INCREMENT", "Value " + (++i));
}

答案 1 :(得分:0)

你可以用不同的方式使用你的按钮的onTouch()或onFocusChange()听众。我不确定你只试一次。

例如:

        button.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub

        }
    });

    button.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return false;
        }
    });

答案 2 :(得分:0)

@Override
 public boolean onTouch(View view, MotionEvent motionEvent) {
  // TODO Auto-generated method stub

  textX.setText("x: " + String.valueOf(motionEvent.getX()));
  textY.setText("y: " + String.valueOf(motionEvent.getY()));

  int action = motionEvent.getAction();

  switch (action){
  case MotionEvent.ACTION_DOWN:
   textEvent.setText("ACTION_DOWN");      
   break;
  case MotionEvent.ACTION_MOVE:
   textEvent.setText("ACTION_MOVE");
   break;
  case MotionEvent.ACTION_UP:
   textEvent.setText("ACTION_UP");          // you need this!
   break;
  case MotionEvent.ACTION_CANCEL:
   textEvent.setText("ACTION_CANCEL");
   break;
  default:
   textEvent.setText("Unknown!");
  }

  return true; //means event have been processed
 }