触摸按钮没有声音
如果我使用:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这样做对我有用:
Doing this works for me:
请问,在什么条件下可以访问 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.
尝试将
android:soundEffectsEnabled="true"
添加到Try adding
android:soundEffectsEnabled="true"
to your<Button>
tag.你的问题不太清楚你想何时播放声音,但我回答你的问题假设你想在 Action_DOWN 发生时播放它。如果你打算创建一个切换按钮,你的代码应该看起来像这样
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
以下行将播放点击声音:
Following line will play the click sound:
有两种听声音的方式
1 使用内置声音
使用自定义声音
播放用户定义的声音
There are two ways to listen sound
1 use inbuilt sound
use custom sound
play the user defined sound