Android如何同时使用OnClick和Long按(3秒)进行相同的按钮

时间:2016-06-02 16:20:06

标签: android button onclicklistener ontouchlistener

在我的application我需要为Action A正常click执行button,并且还需要执行其他Action B相同的按钮超过3秒。 我试过以下代码

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;

                    }
                });

但它没有像我期望的那样工作,任何人都可以指导我完成这项操作。

2 个答案:

答案 0 :(得分:3)

贾马尔。 你真的需要等待3秒吗?

如果您不需要等待3秒,请使用setOnClickListener方法进行单击操作,使用setOnLongClickListener进行长时间单击操作。

您可以在以下链接中查看:

https://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)

https://developer.android.com/reference/android/view/View.html#setOnLongClickListener(android.view.View.OnLongClickListener)

-

如果您确实需要等待3秒,则必须检查ACTION_UP和ACTION_DOWN的时间戳之间的差异。

编辑:

public class YourClass {    
private MotionEvent mLastEvent = MotionEvent.ACTION_UP;

final Runnable runr = new Runnable() {

                    @Override
                    public void run() {
                        if(mLastEvent == MotionEvent.ACTION_MOVE) {
                            // Your code to run on long click
                        }
                    }
                };

final Handler handel = new Handler();
contactButton.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        mLastEvent = arg1.getAction();
        switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    handel.postDelayed(runr,3000);
                    break;          
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                default:
                    handel.removeCallbacks(runr);
                    break;

            }
                return true;
        }
    });

}

答案 1 :(得分:2)

所有答案和评论都是真实的。这是您的实施

Button button;
long down,up;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button);
    button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()){
                case MotionEvent.ACTION_DOWN :
                    Toast.makeText(MainActivity.this, "Down", Toast.LENGTH_SHORT).show();
                    down=System.currentTimeMillis();
                    break;
                case MotionEvent.ACTION_UP :
                    Toast.makeText(MainActivity.this, "Up", Toast.LENGTH_SHORT).show();
                    up=System.currentTimeMillis();
                    if(up-down>3000)
                        Toast.makeText(MainActivity.this, "More than 3", Toast.LENGTH_SHORT).show();
                    return true;
            }
            return false;
        }
    });
}

}