2

可能重复:
Android 中的 onPress/onRelease

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

是否有一个事件可以捕捉到这一点?

我尝试搜索类似 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);
        }
    }
}

但这仅在用户将手指放在按钮上方时才有效,当他将其拖动到屏幕上的其他位置时,计数器会继续运行,有什么想法吗?

4

3 回答 3

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));
}
于 2012-09-11T16:00:59.107 回答
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;
        }
    });
于 2012-09-11T11:27:10.617 回答
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
 }
于 2012-09-11T11:34:13.240 回答