[英]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.