简体   繁体   English

如何启用/禁用ToggleButton的声音?

[英]How to enable/disable sound from ToggleButton?

I'm trying to make it in my app where if a user goes to my settings activity he could click on a ToggleButton and turn off the in-app audio.I'm trying to use the compound button method to accomplishing this but from what I found is this, 我试图在我的应用程序中进行设置,如果用户进入我的设置活动,他可以单击ToggleButton并关闭应用程序内音频。我正在尝试使用复合按钮方法来实现此目的,但是我发现是这个

soundnotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if (isChecked) {
                // Sound Notifications is enabled
            } else {
                // Sound Notifications is disabled
            }
        }
    });

What exactly am I supposed to put into the // portion of the code? 我应该在代码的//部分中放入什么? I found this off the android website but it doesn't exactly explain what I'm supposed to put here to enable/disable the sound from the ToggleButton.I already specified in the XML the OnClick,TextOff and TextON so I'm just asking what am I supposed to do in the Java? 我在android网站上找到了它,但它并不能完全解释我应该在此处启用/禁用ToggleButton声音的内容。我已经在XML中指定了OnClick,TextOff和TextON,所以我只是问我应该在Java中做什么?

Thank You 谢谢

Try this code: 试试这个代码:

soundnotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if (isChecked) {
                // Sound Notifications is enabled
                soundnotify.setSoundEffectsEnabled(true);
            } else {
                // Sound Notifications is disabled
                soundnotify.setSoundEffectsEnabled(false);
            }
        }
    });

您可以在布尔值SharedPreferences存储一个布尔变量,并在每次需要声音时进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM