ACTION_UP无效,但ACTION_DOWN为

时间:2017-03-06 14:47:31

标签: java android motionevent ontouch

我的代码用于在按下按钮时播放指定的频率和波形,并在释放按钮时停止播放,但释放按钮时声音不会停止,任何帮助都会有用:

public boolean onTouch(View v, MotionEvent event) {
    int frequency = Integer.parseInt(frequencyInput.getText().toString());
    displayFrequency.setText(String.valueOf(frequency));
    sineWave.setSine(frequency);
    squareWave.setSquareWave(frequency);
    sawWave.setSawWave(frequency);
    boolean on = (sine.isChecked());
    boolean sqOn = (square.isChecked());
    boolean sawOn = (saw.isChecked());
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (on) {
                sineWave.start();
            } else if (sqOn) {
                squareWave.start();
            } else if (sawOn) {
                sawWave.start();
            }
            break;
        case MotionEvent.ACTION_UP:
            if (!on) {
                sineWave.stop();
            }
            if (!sqOn) {
                squareWave.stop();
            }
            if (!sawOn) {
                sawWave.stop();
            }
            break;
   }
   return false;
}}

0 个答案:

没有答案