触摸按钮没有声音

发布于 2024-12-31 21:39:46 字数 921 浏览 1 评论 0原文

如果我使用:

    final Button btnNught = (Button) findViewById(R.id.night);
    btnNught.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            btnNught.setPressed(true);
        }
    });

如果我单击按钮,我会听到点击声音,但如果我使用:

    btnNught.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (btnNught.isPressed()) {
                btnNught.setPressed(false);
                return true;
            }
            else {
                btnNught.setPressed(true);
                return false;
            }
        }
    });

我不会听到任何触摸声音。 我哪里搞错了?

编辑: 我想创建像切换按钮一样具有两种状态的按钮:按下和未按下当然有两种不同的颜色(正常浅灰色未按下和按下时绿色)。

在这种情况下(上图),当我触摸按钮时,我不会听到任何声音,但如果我使用 onClick 方法,我会听到“点击”声音。 当我使用 onTouch 方法时,如何获得“点击”或“触摸”声音?

If I use:

    final Button btnNught = (Button) findViewById(R.id.night);
    btnNught.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            btnNught.setPressed(true);
        }
    });

And if I click on button I listen click sound, but if I use:

    btnNught.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (btnNught.isPressed()) {
                btnNught.setPressed(false);
                return true;
            }
            else {
                btnNught.setPressed(true);
                return false;
            }
        }
    });

I do not listen any sound on touch.
Where I made mistake?

Edit:
I want to create button like togglebutton with two state: pressed and non-pressed with two different colors of course (normal-light gray non pressed and green when pressed).

In this case (above), when I touch button I do not listen any sound, but if I use onClick method I listen 'click' sound.
How can I get 'click' or 'touch' sound when I use onTouch method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

因为看清所以看轻 2025-01-07 21:39:46

这样做对我有用:

btnNught.setSoundEffectsEnabled(true);
btnNught.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
       if(event.getAction()!=MotionEvent.ACTION_UP ||        
          event.getAction()!=MotionEvent.ACTION_DOWN) 
       {
           return false;
       }
       if(event.getAction()==MotionEvent.ACTION_DOWN)
       {
            btnNught.playSoundEffect(SoundEffectConstants.CLICK);
            //Set whatever color you want to set
       }
       else 
       {

       }
       return true;
    }
});

Doing this works for me:

btnNught.setSoundEffectsEnabled(true);
btnNught.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
       if(event.getAction()!=MotionEvent.ACTION_UP ||        
          event.getAction()!=MotionEvent.ACTION_DOWN) 
       {
           return false;
       }
       if(event.getAction()==MotionEvent.ACTION_DOWN)
       {
            btnNught.playSoundEffect(SoundEffectConstants.CLICK);
            //Set whatever color you want to set
       }
       else 
       {

       }
       return true;
    }
});
羁〃客ぐ 2025-01-07 21:39:46

请问,在什么条件下可以访问 btnNught.setPressed(true);在第二个例子中?据我所知,你根本无法按下按钮。在“按下操作”时,您会消耗该操作,因此不会按下按钮。

编辑:在侦听器的开头放置一个断点,并在按钮使用的不同变体中输入它。并检查行为。

Please, on what conditions could you get to the btnNught.setPressed(true); in the second example? As I see, you simply can't press the button. On Action Down you consume the action, so button won't be pressed.

Edit: Put a breakpoint at the start of listener and enter it in the different variants of the button use. And check the behaviour.

迷雾森÷林ヴ 2025-01-07 21:39:46

尝试将 android:soundEffectsEnabled="true" 添加到

Try adding android:soundEffectsEnabled="true" to your <Button> tag.

飘然心甜 2025-01-07 21:39:46

你的问题不太清楚你想何时播放声音,但我回答你的问题假设你想在 Action_DOWN 发生时播放它。如果你打算创建一个切换按钮,你的代码应该看起来像这样

btnNught.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
       if(event.getAction()!=MotionEvent.ACTION_UP ||        
               event.getAction()!=MotionEvent.ACTION_DOWN) 
       {
             return false;
       }
       if(event.getAction()==MotionEvent.ACTION_DOWN)
       {
          btnNught.setPressed(true);
          //Set whatever color you want to set

       }
       else 
       {
          btnNught.setPressed(false);
       }
        return true;
    }
});

Your question is not very clear over when you want to play the sound but I am answering your question assuming that you wanna play it when the Action_DOWN occurs.If you plan to create a toggle button your code should look something like this

btnNught.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
       if(event.getAction()!=MotionEvent.ACTION_UP ||        
               event.getAction()!=MotionEvent.ACTION_DOWN) 
       {
             return false;
       }
       if(event.getAction()==MotionEvent.ACTION_DOWN)
       {
          btnNught.setPressed(true);
          //Set whatever color you want to set

       }
       else 
       {
          btnNught.setPressed(false);
       }
        return true;
    }
});
忱杏 2025-01-07 21:39:46

以下行将播放点击声音:

btnNught.playSoundEffect(SoundEffectConstants.CLICK);

Following line will play the click sound:

btnNught.playSoundEffect(SoundEffectConstants.CLICK);
愿与i 2025-01-07 21:39:46

有两种听声音的方式

1 使用内置声音

Go to Settings -> Sound -> 
And do the following settings 

sound profile set **Normal**
Touch sound set **ON**
  1. 使用自定义声音

    播放用户定义的声音

There are two ways to listen sound

1 use inbuilt sound

Go to Settings -> Sound -> 
And do the following settings 

sound profile set **Normal**
Touch sound set **ON**
  1. use custom sound

    play the user defined sound

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文