简体   繁体   English

使用按钮更改另一个活动的背景颜色

[英]Change background color of another activity with button

I have two activities main and settings.我有两个主要活动和设置。 I want to change the background color of the main using a button that's on settings.我想使用设置上的按钮更改主体的背景颜色。 I tried using RelativeLayout and get the layout of the main using the id and changing the color when touching the button but it doesn't work.我尝试使用 RelativeLayout 并使用 id 获取主要布局并在触摸按钮时更改颜色但它不起作用。 I also tried doing with Intent but I don't know how to do it.我也尝试过使用 Intent 但我不知道该怎么做。 Got the code for the settings activity below获得了下面设置活动的代码

public class Settings extends AppCompatActivity {
    private Button changeColorButton;
    private RelativeLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        changeColorButton = findViewById(R.id.changeColorButton);
        layout = findViewById(R.id.mainLayout);
        /** Called when the user taps the Settings button */
        changeColorButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View V){
                Intent intent = new Intent(Settings.this, Main.class);
                startActivity(intent);
                layout.setBackgroundColor(Color.BLUE);
            }
        });
    }
}

Any help please?有什么帮助吗?

You cannot change views of another activity like that.您不能像那样更改其他活动的视图。

You can send an 'extra' parameter with your Intent and read it on Settings activity.您可以发送带有 Intent 的“额外”参数,并在“设置”活动中读取它。 You can change the color based on the value you receive via the intent.您可以根据通过意图收到的值更改颜色。

Or if its a more permanent setting, you can save it on SharedPreferences and read it on Main.或者,如果它是一个更永久的设置,您可以将其保存在 SharedPreferences 上并在 Main 上阅读。

暂无
暂无

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

相关问题 单击按钮并更改另一个按钮的背景颜色 - Click Button and change background color of another button 如何从另一个活动更改一个活动的背景颜色? - How do I change the background color of an activity from another activity? 如何通过另一个活动中的按钮更改活动的背景色 - How to change background colour of an activity via button from another activity 按下另一个活动中的按钮时更改按钮颜色 - Change button color when a button in another activity is pressed 更改单击时按钮的背景颜色,然后打开一个新活动 - Change background color of button on click and then open a new activity 如何传递按钮意图在另一个活动中制作可绘制的变色? - how to pass button intent to make drawable change color in another activity? 如何通过在另一个活动中单击按钮来更改TextView的颜色? - How to change the color of a TextView in one activity by clicking a button in another? 如何更改按钮上活动的背景颜色单击从单选按钮中选择颜色的位置 - how to change the background color of activity on button click where color is picked from radio button 在第二个活动中单击一个按钮时,如何使用微调器更改主要活动的背景颜色? - How can I change the background color of a Main Activity by using spinner while clicking a button in the Second Activity? 如何通过按下另一个活动按钮上的按钮来永久更改我的按钮颜色 - How to change my button color permanent from pressing button on another activity button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM