sách gpt4 ai đã đi

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

In lại 作者:行者123 更新时间:2023-12-01 14:44:24 25 4
mua khóa gpt4 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 {











@Ghi đè
public void onCreate(Bundle savedInstanceState) {
// TODO Phương thức tự động tạo 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() {

@Ghi đè
public void onClick(View v) {
// TODO Phương thức tự động tạo 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);

} khác {
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() {








@Ghi đè
public void onClick(View v) {
// TODO Phương thức tự động tạo 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() {

@Ghi đè
public void onClick(View v) {
// TODO Phương thức tự động tạo stub
startActivity(new Intent("com.fullfrontalgames.numberfighter.Notifications"));
}
});
AccountSettings.setOnClickListener(new View.OnClickListener() {

@Ghi đè
public void onClick(View v) {
// TODO Phương thức tự động tạo stub
startActivity(new Intent("com.fullfrontalgames.numberfighter.AccountSettings"));
}
});









}

1 Câu trả lời

theodoc 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
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com