【发布时间】:2016-06-02 16:20:06
【问题描述】:
在我的application 中,我需要在为button 执行正常click 的同时执行一些Action A,并且还需要在按住相同按钮超过3 秒的同时执行其他一些Action B。
我在下面的代码中试过这个
private boolean isMoved = false;
final Runnable runr = new Runnable() {
@Override
public void run() {
// Your code to run on long click
System.out.println("long pressed for 3 sec");
}
};
final Handler handel = new Handler();
contactButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
isMoved = false;
handel.postDelayed(runr,3000);
break;
case MotionEvent.ACTION_MOVE:
isMoved = true;
case MotionEvent.ACTION_UP:
if(!isMoved)
System.out.println("button single click");
default:
handel.removeCallbacks(runr);
break;
}
return true;
}
});
但它不像我期望的那样工作,请任何人指导我完成此操作。
【问题讨论】:
-
忠告:不要使用
System.out.println使用Log -
@zhelon 当然,我会的
标签: android button onclicklistener ontouchlistener