简体   繁体   English

如何从另一个活动更改一个活动的背景颜色?

[英]How do I change the background color of an activity from another activity?

I have MainActivty and SettingsActivity , when I press a button in my SettingsActivity I want the background color of the MainActivity to change. 我有MainActivtySettingsActivity ,当我在SettingsActivity按下按钮时,我希望MainActivity的背景色发生变化。

How do I achieve this? 我该如何实现? Sorry if this question is a bit dumb, I recently started out and I'm a bit lost. 抱歉,这个问题有点愚蠢,我最近刚开始,有点迷路。

Save the color Hex as String or Integer of Color to SharedPreferences and get it on activity @onCreate() 将颜色十六进制作为字符串或颜色整数保存到SharedPreferences,并在活动@onCreate()上获取它

SharedPreferences preferences= context.getSharedPreferences(
      "SharedPrefName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("SharedPrefKey","#FF0000" /*change this to what you want*/);
editor.apply();

then in activity get color 然后在活动中变色

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String color = preferences.getString("SharedPrefKey", "#000000");

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

相关问题 如何通过另一个活动中的按钮更改活动的背景色 - How to change background colour of an activity via button from another activity 使用按钮更改另一个活动的背景颜色 - Change background color of another activity with button 如何通过不同的活动更改视图的背景色 - How to change the background color on a view from different activity 如何从 android 中的不同活动更改视图的背景颜色? - How to change the background color of a view from a different activity in android? 如何更改活动的背景颜色与列表视图中的项目? - how to change the background color on activity with items in a listview? 如何使用彩色按钮作为用户选择从第二个活动更改我的 MainActivity 的背景颜色? - How can I change the background color of my MainActivity from a second activity using colored buttons as user choices? 在第二个活动中单击一个按钮时,如何使用微调器更改主要活动的背景颜色? - How can I change the background color of a Main Activity by using spinner while clicking a button in the Second Activity? 如何从处理程序更改活动? - How do I change a Activity from a Handler? 如何将活动移动到另一个活动? - How do I move activity to another activity? 通过共享首选项更改其他活动onCheckedChange上按钮的背景颜色(?) - Change background color of buttons from another activity onCheckedChange through shared preference(?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM