gpt4 book ai didi

java - 如何将首选项与 Activity 联系起来?

转载 作者:行者123 更新时间:2023-11-29 02:41:47 26 4
gpt4 key购买 nike

Android Studio 的新手,我在将我的 preferences.xml 中的 CheckBox 与我的所有其他 Activity 连接时遇到问题,因此它们充当应用程序的设置。正如官方页面 ( https://developer.android.com/guide/topics/ui/settings.html ) 所说,我已经设置了所有内容,但我仍然无法让所有内容正常工作。

这是我的设置类:

public class Settings extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}

和我的 SettingsFragment 类:

public class SettingsFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}

我想做的是在应用程序周围打开或关闭音乐或 sfx,这是我的 preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<CheckBoxPreference
android:key="music check box"
android:title="Music"
android:defaultValue="true"
android:id="@+id/checkMusic"/>

<CheckBoxPreference
android:key="sfx check box"
android:title="SFX"
android:defaultValue="true"
android:id="@+id/checkSfx"/>

<CheckBoxPreference
android:key="notifications check box"
android:title="Notifications"
android:defaultValue="true"
android:id="@+id/checkNotif"/>

</PreferenceScreen>

现在我的 MediaPlayer 位于应用程序类中,因此它会遍历每个 Activity 。我该如何处理这一切?

最佳答案

Now my MediaPlayer is located in an Application class so it would go around every activity

这可能不是正确的模式。

How do I go about all of this?

让管理您的 MediaPlayer 的组件(Application 就其本身而言,稍后可能是一个 Service)调用 registerOnSharedPreferenceChangeListener() 上的 SharedPreferences 以了解更改。然后,根据需要对这些更改使用react。

关于java - 如何将首选项与 Activity 联系起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43572194/

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