View的双击动作
有时在android中需要为某一控件设置双击监听,实现也挺简单,自己动手吧。编码永远不是问题,思路才是最重要。
public class DoubleClickDemo extends Activity { private String TAG = "DoubleClickDemo"; private int count = 0; private long first = 0; private long second = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_location); private View view = findViewById(R.id.button1); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { count++; if (count == 1) { first = System.currentTimeMillis(); view.setOnTouchListener(new ViewOnTouchListener()); } else if (count == 2) { second = System.currentTimeMillis(); // 计算两次点击时间差,小于500毫秒判定为双击动作 if ((second - first) <= 500) { // 双击待执行动作 Log.e(TAG, "DoubleClick"); } // 清空数据,为下一次监听做准备 count = 0; first = 0; second = 0; } } }); } }