简体   繁体   English

如何在 Android Studio 的所有页面上保存功能(即 SFX 或音乐等设置)?

[英]How to save a feature (i.e. setting like SFX or Music) across all pages in Android Studio?

I am wondering how to save a feature across all pages when flipping between them in Android Studio.我想知道如何在 Android Studio 中在它们之间翻转时跨所有页面保存功能。

I am trying to make a rigged dice rolling app that I want a setting to stick with the app until I turn it off.我正在尝试制作一个操纵骰子滚动应用程序,我想要一个设置来坚持应用程序,直到我关闭它。

Current code for the page where I want to have the setting save:我想要保存设置的页面的当前代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


public void showToast(View view) {
    Toast toast = Toast.makeText(this, "You've switched a switch", Toast.LENGTH_SHORT);
    toast.show();
}


public void changeHomeScreen(View view) {
    startActivity(new Intent(getApplicationContext(), HomeScreen.class));
}
public void trollMode (View view){
    Switch s = findViewById(R.id.switch3);
    ImageView i = findViewById(R.id.imageView2);
    Button a = findViewById(R.id.button4);
    Button b = findViewById(R.id.button7);
    Button c = findViewById(R.id.button8);
    Button d = findViewById(R.id.button9);
    Button e = findViewById(R.id.button10);
    Button f = findViewById(R.id.button11);

    a.setVisibility(View.INVISIBLE);
    b.setVisibility(View.INVISIBLE);
    c.setVisibility(View.INVISIBLE);
    d.setVisibility(View.INVISIBLE);
    e.setVisibility(View.INVISIBLE);
    f.setVisibility(View.INVISIBLE);
    if (s.isChecked()){
        i.setImageResource(R.drawable.trollface);
        Toast toast = Toast.makeText(this, "You have ENABLED Troll Mode!", Toast.LENGTH_SHORT);
        toast.show();
        MediaPlayer myAudio = MediaPlayer.create(MainActivity.this, R.raw.evillaugh);
        myAudio.start();
    } else {
        i.setImageResource(R.drawable.dices);
        Toast toast = Toast.makeText(this, "You have DISABLED Troll Mode", Toast.LENGTH_SHORT);
        toast.show();
    }
 }
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


public void showToast(View view) {
    Toast toast = Toast.makeText(this, "You've switched a switch", Toast.LENGTH_SHORT);
    toast.show();
}


public void changeHomeScreen(View view) {
    startActivity(new Intent(getApplicationContext(), HomeScreen.class));
}
public void trollMode (View view){
    Switch s = findViewById(R.id.switch3);
    ImageView i = findViewById(R.id.imageView2);
    Button a = findViewById(R.id.button4);
    Button b = findViewById(R.id.button7);
    Button c = findViewById(R.id.button8);
    Button d = findViewById(R.id.button9);
    Button e = findViewById(R.id.button10);
    Button f = findViewById(R.id.button11);

    a.setVisibility(View.INVISIBLE);
    b.setVisibility(View.INVISIBLE);
    c.setVisibility(View.INVISIBLE);
    d.setVisibility(View.INVISIBLE);
    e.setVisibility(View.INVISIBLE);
    f.setVisibility(View.INVISIBLE);
    if (s.isChecked()){
        i.setImageResource(R.drawable.trollface);
        Toast toast = Toast.makeText(this, "You have ENABLED Troll Mode!", Toast.LENGTH_SHORT);
        toast.show();
        MediaPlayer myAudio = MediaPlayer.create(MainActivity.this, R.raw.evillaugh);
        myAudio.start();
    } else {
        i.setImageResource(R.drawable.dices);
        Toast toast = Toast.makeText(this, "You have DISABLED Troll Mode", Toast.LENGTH_SHORT);
        toast.show();
   }
 }

} }

This is for the switch called "Troll Mode" which my method is called.这是我的方法被称为“巨魔模式”的开关。 I want, when it is switched on, to make buttons appear, and then when you click those buttons, I want it to reroute you to my home page.我希望,当它打开时,显示按钮,然后当您单击这些按钮时,我希望它重新路由您到我的主页。 When you roll the dice on the home page while troll mode is on, then it will only show the number that you picked and nothing else until you turn troll mode off.当您在巨魔模式开启的情况下在主页上掷骰子时,它只会显示您选择的数字,而不会显示其他任何内容,直到您关闭巨魔模式。

If anybody needs more information about the problem, I will be happy to give you what you need to the best of my ability.如果有人需要有关该问题的更多信息,我将很乐意尽我所能为您提供所需的信息。

I am trying to make a rigged dice rolling app that I want a setting to stick with the app until I turn it off.我正在尝试制作一个操纵骰子滚动应用程序,我想要一个设置来坚持应用程序,直到我关闭它。

Store the setting using SharedPreferences .使用SharedPreferences存储设置。

See the training documentation for a guide on how to use SharedPreferences .有关如何使用SharedPreferences的指南,请参阅培训文档

And if you want to expose the setting to the user, use a PreferenceFragment .如果您想向用户公开设置,请使用PreferenceFragment

暂无
暂无

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

相关问题 Android Studio 游戏中所有活动的不间断背景音乐 - Uninterrupted background music across all activities in Android Studio game 迁移到旧版Android Studio(即3.1.2)上的androidx? - Migrate to androidx on older version of android studio i.e. 3.1.2? 如何在 Android Studio 中跨活动保存变量值 - How do I save variable values across Activities in Android Studio 在 Android Wear OS(即 CSV)上保存和传输智能手表传感器数据的最佳方式是什么? - What is the best way to save and transfer smartwatch sensor data on Android Wear OS (i.e. as CSV)? Android Studio 中的 WebView 无法正确显示网站(即 www.google.com 没有徽标、搜索栏和屏幕上方的所有内容) - WebView in Android Studio does not show sites properly (i.e. www.google.com is without logo, search bar and all the stuff on upper screen) Spring Data Mongo:如何仅保存日期,即不保存时间 - Spring Data Mongo: How to save only date i.e. not time 如何找到对Java类的所有引用(即,依赖关系,传入耦合)? - How do I find all references (i.e., dependencies, afferent couplings) to a Java class? 如何在较旧的 Android 设备(即 Gingerbread 和 Froyo)上显示首选项屏幕? - How can I show a preferences screen on older Android devices i.e. Gingerbread and Froyo? 我如何在Android的“偏好设置”中添加一些文本(即某种“关于”信息) - How can i add some text (i.e. kind of “about” info) in Preferences in Android 如何在 Spring MVC 中捕获所有未处理的异常(即没有现有的 @ExceptionHandler)? - How to catch all unhandled exceptions (i.e. without existing @ExceptionHandler) in Spring MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM