实现我自己的按钮监听器

时间:2016-02-28 06:56:03

标签: java android

您好我想在Android上实现自己的Button Listener Interface。它位于OnClickListener和OnLongClickListener接口之间。

我创建了OnMyTouchListener接口:

public interface OnMyTouchListener {
  public static int duration = 1;
  public void onMyTouch(View view);
}

我伸出按钮:

public class Button3D extends Button {
  public Button3D(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public void addMyTouchListener(OnMyTouchListener onMyTouchListener){
      //  ???
  }
}

我希望在触摸持续时间超过1秒且小于2秒时调用onMyTouch方法,但无法找到解决方案。

1 个答案:

答案 0 :(得分:2)

让你开始的想法:

在Button3D中你有一个OnMyTouchListener(可能是List)的引用 在Button3D.addMyTouchListener中,您只需将Object添加到List。

现在棘手的部分:
- 单击Button时,保存System.currentNanos()
- 释放按钮时,计算经过的时间并将其与两个值进行比较。它介于两者之间,为所有监听器启动onMyTouch。