【问题标题】:Android how to use both OnClick and Long press(3 sec) for same ButtonAndroid如何对同一个按钮同时使用OnClick和长按(3秒)
【发布时间】: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;

                    }
                });

但它不像我期望的那样工作,请任何人指导我完成此操作。

【问题讨论】:

标签: android button onclicklistener ontouchlistener


【解决方案1】:

贾马尔。 你真的需要等待 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;
        }
    });

}

【讨论】:

  • 是的,我只需要长按 3 秒。不是默认的 Android Longclick
  • 因此,不要使用处理程序,而是存储与 ACTION_DOWN 事件关联的时间,并在触发 ACTION_UP 时检查它们之间的差异。如果大于等于 3 秒可以认为是长点击事件。
  • 你的解决方案很好,是否有可能在按钮按住达到3秒时触发操作。意味着当时间间隔达到3秒时做一些工作。根据您的解决方案,它将计算用户拿手指的时间间隔。因此,如果用户点击按钮超过 3 秒(例如 10 秒),则在他移开手指之前不会执行任何操作。
【解决方案2】:

所有答案和 cmets 都是正确的。这是你的实现

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;
        }
    });
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多