我在我的设置活动中有这样的情况,当用户点击ToggleButton时,它应该在我的应用程序中静音,但它不起作用。我在Tutorial类中放置的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"));
}
});
}
答案 0 :(得分:1)
根据setSoundEffectsEnabled
的doc设置此视图是否应为事件启用声音效果 如点击和触摸。
如果您已经播放,可能希望为视图禁用声音效果 听起来,例如,播放dtmf音调的拨号键。
因此,此功能应设置为关闭视图的声音效果(如单击或触摸)。不要关闭设备声音。
为了您的目的,请检查AudioManager