Android 如何在AlertDialog上禁用系统声音效果?
我有一个带有两个按钮的AlertDialog。我希望他们在单击时播放我的自定义声音。所以我在每个按钮上都有这个代码:Android 如何在AlertDialog上禁用系统声音效果?,android,android-alertdialog,audio,Android,Android Alertdialog,Audio,我有一个带有两个按钮的AlertDialog。我希望他们在单击时播放我的自定义声音。所以我在每个按钮上都有这个代码: SoundUtility.getInstance(Add_Edit_Note.this).playPositive(); SoundUtility是我编写的用于播放自定义声音的类。 问题是:它确实播放我的自定义声音,但同时也播放系统音效,所以我有两种声音同时播放。我可以通过重写按钮在常规按钮上禁用它: public class AppButton extends Button
SoundUtility.getInstance(Add_Edit_Note.this).playPositive();
SoundUtility是我编写的用于播放自定义声音的类。
问题是:它确实播放我的自定义声音,但同时也播放系统音效,所以我有两种声音同时播放。我可以通过重写按钮在常规按钮上禁用它:
public class AppButton extends Button {
public AppButton(Context context, AttributeSet attrs) {
super(context, attrs);
// Disable sound effect
this.setSoundEffectsEnabled(false);
}
}
然后在我的XML文件中:
<com.my.app.AppButton
... />
}
编辑2
我的AlertDialog的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to cancel?")
.setCancelable(false) // The dialog is modal, a user must provide an answer
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
// If the answer is Yes
public void onClick(DialogInterface dialog, int id) {
...
setResult(RESULT_CANCELED); // Setting result as cancelled and returning it to main activity
SoundUtility.getInstance(Add_Edit_Note.this).playPositive(); // Play positive sound
Add_Edit_Note.this.finish(); // Closing current activity
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
// If the answer is No
public void onClick(DialogInterface dialog, int id) {
SoundUtility.getInstance(Add_Edit_Note.this).playNegative(); // Play negative sound
dialog.cancel(); // Closing the confirmation dialog
}
});
builder.create().show(); // Present the dialog to the user
试试这个
Button btn = dialog.getButton(Dialog.BUTTON_POSITIVE);
btn.setSoundEffectsEnabled(false);
调用setSoundEffectsEnabled
,查看您拥有的所有按钮
编辑:
而不是
builder.create().show();
使用
通过在主题中加入android:soundEffectsEnabled=false,我可以在全球范围内关闭声音反馈
您可以从清单文件将主题应用于整个应用程序。 替代方法: 您可以创建一个类并在布局文件中使用它 你们班:
package com.me.customeapp
public class MeTextView extends TextView {
public MeTextView (Context context, AttributeSet attrs) {
this.setSoundEffectsEnabled(false);
}
}
在xml文件中输入以下代码:
<com.me.customeapp.TextView
...
</com.me.customeapp.TextView>
我想你应该为你的SoundUtility类发布代码,它一定在某处播放这个系统音效。@breadbin发布了SoundUtility代码。但实际上里面没有播放系统声音效果的东西,只有我的自定义声音…我应该把代码放在哪里?我在对话框的onClick方法中尝试了它,但是我得到了一个错误,说“类型DialogInterface的getButton(int)方法未定义”。我尝试将“dialog”转换为“((AlertDialog)dialog).getButton”,它消除了错误消息,但我仍然具有系统声音效果。请在创建对话框之后和显示之前放置它。同样在你创建对话框的地方张贴代码,你应该把按钮改成你正在使用的任何类型的按钮,仍然没有运气。。。我用我的AlertDialog代码更新了这个问题,我应该把这个代码放在哪里?试过了,首先我得到了getButton方法的错误:类型对话框的getButton(int)方法未定义。它建议添加cast,我做了,它删除了错误,但随后应用程序崩溃了。像我做的一样,将对话框更改为AlertDialog,也发布崩溃日志,我从未使用过主题文件。。。我在哪里找到它?你可以从清单文件将主题应用到整个应用程序。我已经更新了我的帖子。您可以查看它,我想我的应用程序在清单中应用了一个主题:。。。但是我不知道在哪里插入这一行android:soundEffectsEnabled=false
。我尝试了几个地方(清单本身、AppTheme文件…),但遇到了错误消息。
package com.me.customeapp
public class MeTextView extends TextView {
public MeTextView (Context context, AttributeSet attrs) {
this.setSoundEffectsEnabled(false);
}
}
<com.me.customeapp.TextView
...
</com.me.customeapp.TextView>