gpt4 book ai didi

android - 从另一个 Activity 更改一个 Activity 的背景

转载 作者:行者123 更新时间:2023-11-29 01:36:59 25 4
gpt4 key购买 nike

示例 我有 3 个 Activity :activity1、activity2 和 activity3。 activity1 包含一个按钮,单击该按钮可以更改 activity2 和 activity3 的背景。这可能吗?如果是如何?我对使用该方法有一个想法:

//example for activity1

public void onClick(View v) {
View background = findViewById(R.id.activity1relativeLayout);
background.setBackgroundResource(R.drawable.customBackground);

可用于单个 Activity 。使用单个按钮来操纵多个 Activity 怎么样?

最佳答案

您可以在单击按钮时将颜色保存在 SharedPreferences 中。然后在颜色更改 Activity 的 onStart 中读取首选项并更改背景颜色。

Button.onClick(...)

SharedPreferences sharedPref = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("background_resource", selected_background);
editor.apply();

Activity.onStart(...)

SharedPreferences sharedPref = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
int bg = sharedPref.getInt("background_resource", android.R.color.white); // the second parameter will be fallback if the preference is not found
getWindow().setBackgroundDrawableResource(bg);

关于android - 从另一个 Activity 更改一个 Activity 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27338691/

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