OnLongClick按钮监听器android

时间:2014-08-18 19:35:42

标签: android

您好:)我正在尝试检测用户何时按下按钮并且当他释放长按时这样我正在使用此答案:https://stackoverflow.com/a/10746549/3953319 我不知道为什么但是LongClickListener被调用了两次,这是我的代码:

button1.setOnLongClickListener(new View.OnLongClickListener() {
    public boolean onLongClick(View pView) {
        TimeCounter = 0;
        final Random rand = new Random();
        time = rand.nextInt(7) + 1;
        SecTime = time * 1000;
        CountDownTimer2 = new CountDownTimer(SecTime, 1000) {
            public void onTick(long millisUntilFinished) {
            }

            public void onFinish() {
                mPlayer = MediaPlayer.create(MainActivity.this,
                            R.raw.windows_8_notify);
                mPlayer.start();
                t = new Thread() {
                    @Override
                    public void run() {
                        try {
                            while (!isInterrupted()) {
                                Thread.sleep(1);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        TimeCounter++;
                                        textview.setText("Your reaction time:"
                                                    + TimeCounter + "MS");
                                    }
                                });
                            }
                        } catch (InterruptedException e) {
                        }
                    }
                };
                t.start();
            }
        }.start();
        isSpeakButtonLongPressed = true;
        return true;
    }
});

button1.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View pView, MotionEvent pEvent) {
        pView.onTouchEvent(pEvent);
        if (pEvent.getAction() == MotionEvent.ACTION_UP) {
            if (isSpeakButtonLongPressed) {
                if (mPlayer == null) {
                    textview.setText("You have released the button before the sound");
                    CountDownTimer2.cancel();
                } else {
                    t.interrupt();
                    t = null;
                    OldScore = sharedpreferences.getInt("HighScore", 0);
                    if (TimeCounter < OldScore) {
                        Editor editor = sharedpreferences.edit();
                        editor.putInt("HighScore", TimeCounter);
                        editor.commit();
                        textview1.setText("Your new High Score:"
                                    + TimeCounter + "MS");
                    }
                    textview.setText("Your reaction time:"
                                + TimeCounter + "MS");
                    mPlayer.pause();
                    mPlayer.reset();
                    mPlayer = null;
                }
                isSpeakButtonLongPressed = false;
            }
        }
        return false;
    }
});

我运行了应用程序,当我长按该按钮时,我听到&#34; windows_8_notify&#34;两次,为什么?有没有更好的方法呢?

1 个答案:

答案 0 :(得分:3)

我认为这是因为在onTouchListener onTouch方法中,您拨打pView.onTouch(...),然后在return false结束。 return false行表示您的onTouchListener未处理onTouch事件,因此它会调用默认的onTouch函数,该函数与您的{{1}相同打电话。但是,默认功能恰好也会调用pView.onTouch(...)的{​​{1}}方法。因此,每次调用onLongClickListener的{​​{1}}方法时,它会调用onLongClick两次。

SO。如果您在适当的情况下在pView.onTouch(...)方法中返回onLongClickListener,我相信您可以避免这种情况。在我看来,它看起来像这样:

onLongClick

完全重写

true

这将(应该)停止按钮调用onTouch,而不会干扰其他点击功能。

(事实上,我认为你不需要布尔变量button1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View pView, MotionEvent pEvent) { boolean longClickOccurred = pView.onTouchEvent(pEvent); ... if (pEvent.getAction()...) { if (isSpeakButtonLongPressed) { ... longClickOccurred = true; } } return longClickOccurred; } }); ,因为当你断言pEvent.getAction()== MotionEvent.ACTION_UP时,它的值基本上与button1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View pView, MotionEvent pEvent) { boolean longClickOccurred = pView.onTouchEvent(pEvent); if (pEvent.getAction() == MotionEvent.ACTION_UP) { if (isSpeakButtonLongPressed) { if (mPlayer == null) { textview.setText("You have released the button before the sound"); CountDownTimer2.cancel(); } else { t.interrupt(); t = null; OldScore = sharedpreferences.getInt("HighScore", 0); if (TimeCounter < OldScore) { Editor editor = sharedpreferences.edit(); editor.putInt("HighScore", TimeCounter); editor.commit(); textview1.setText("Your new High Score:" + TimeCounter + "MS"); } textview.setText("Your reaction time:" + TimeCounter + "MS"); mPlayer.pause(); mPlayer.reset(); mPlayer = null; } isSpeakButtonLongPressed = false; longClickOccurred = true; } } return longClickOccurred; } }); 相同。除非1)您在pView.onTouchEvent(pEvent)函数之外使用isSpeakButtonLongPressed,或2)您还有其他longClickOccurred集或类似内容,这将导致isSpeakButtonLongPressed返回{{ 1}}没有发生长时间点击。)

无论如何,我希望工作/帮助/不是一个错综复杂的解释。祝你好运!