gpt4 book ai didi

java - 打开/关闭声音按钮不起作用

转载 作者:行者123 更新时间:2023-12-01 14:44:24 25 4
gpt4 key购买 nike

我在我的设置 Activity 中发现,当用户单击 ToggleButton 时,它应该在整个应用程序中静音,但它不起作用。我在教程类中放入的 SoundPool onClick 按钮声音仍在 onClick 上播放声音。我已经使用 ToggleButton 指定我的共享首选项。

这是我的代码,

    package com.fullfrontalgames.numberfighter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;


public class Settings extends Activity {











@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);


Button Notifications = (Button) findViewById(R.id.Notifications);
Button Done = (Button) findViewById(R.id.done);
Button AccountSettings = (Button) findViewById(R.id.AccountSettings);
final ToggleButton AT = (ToggleButton) findViewById(R.id.AudioToggle);



AT.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences appPrefs =
getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences",
MODE_PRIVATE);
SharedPreferences.Editor editor = appPrefs.edit();
editor.putBoolean("atpref", AT.isChecked()); //value to store
editor.commit();

if ((AT.isChecked())) {
AT.setSoundEffectsEnabled(true);

} else {
AT.setSoundEffectsEnabled(false);
}
}

});







SharedPreferences appPrefs =
getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences",
MODE_PRIVATE);
boolean atpref = appPrefs.getBoolean("atpref", true); //default is true
AT.setChecked(atpref);
Done.setOnClickListener(new View.OnClickListener() {








@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent Intent = new Intent(Settings.this,activity_main.class);
Intent.setFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(Intent);



}
});
Notifications.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.fullfrontalgames.numberfighter.Notifications"));
}
});
AccountSettings.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.fullfrontalgames.numberfighter.AccountSettings"));
}
});









}

最佳答案

根据doc setSoundEffectsEnabled

Set whether this view should have sound effects enabled for events such as clicking and touching.

You may wish to disable sound effects for a view if you already play sounds, for instance, a dial key that plays dtmf tones.

所以这个函数应该关闭 View 的声音效果(如点击或触摸)。不设置设备声音的开关。

出于您的目的,请检查 AudioManager

关于java - 打开/关闭声音按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15595161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com