简体   繁体   English

如何使用彩色按钮作为用户选择从第二个活动更改我的 MainActivity 的背景颜色?

[英]How can I change the background color of my MainActivity from a second activity using colored buttons as user choices?

I have a program with more than one activity.我有一个包含多个活动的程序。 From my MainActivity I can click a button which will send the user to the ThirdActivity_ColorPicker.在我的 MainActivity 中,我可以单击一个按钮,该按钮会将用户发送到 ThirdActivity_ColorPicker。 In the third activity I have three buttons, named colors, when clicked should change the background color of the MainActivity.在第三个活动中,我有三个按钮,名为 colors,单击时应更改 MainActivity 的背景颜色。 Except, it is not changing the background color.除了,它不会改变背景颜色。

In MainActivity I have an Intent which switches me to the third activity.在 MainActivity 我有一个 Intent 将我切换到第三个活动。

private View.OnClickListener changeToColorPickerActivity = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      Intent goToThirdActivityColorPicker = new Intent(getApplication(), ThirdActivity_ColorPicker.class);
      startActivityForResult(goToThirdActivityColorPicker, COLOR_PICKER_REQUEST);
    }
  };

In my third activity I have the three buttons, red, blue, and green, which when clicked should change the MainActivity background color to the selected color.在我的第三个活动中,我有三个按钮,红色、蓝色和绿色,单击它们时应该将 MainActivity 背景颜色更改为所选颜色。

  private View.OnClickListener changeMainActivityToBlue = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      Intent intentBlue = new Intent();
      setResult(RESULT_CODE_BLUE, intentBlue);
      finish();
    }
  };

Back in MainActivity I have an onActivityResult to receive data from the thrid activity回到 MainActivity 我有一个 onActivityResult 从第三个活动接收数据

  @Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    thirdAct = new ThirdActivity_ColorPicker();

    if(requestCode == RESULT_OK && resultCode == thirdAct.RESULT_CODE_RED){
      constraintLayout.findViewById(R.id.main_layout).setBackgroundColor(getColor(R.color.redBackground));
    }else if(requestCode == RESULT_OK && resultCode == thirdAct.RESULT_CODE_GREEN){
      constraintLayout.findViewById(R.id.main_layout).setBackgroundColor(getColor(R.color.greenBackground));
    }else if(requestCode == RESULT_OK && resultCode == thirdAct.RESULT_CODE_BLUE){
      constraintLayout.setBackgroundColor(getColor(R.color.blueBackground));
    }
  }

Result I would like is for the background color to change with the code above, but used correctly because I know I am doing something wrong somewhere.我想要的结果是背景颜色随着上面的代码而改变,但使用正确,因为我知道我在某处做错了。 Thank you.谢谢你。

In your ThirdActivity_ColorPicker , change the code on each of your color button click, that is (Red, Green, Blue, etc), as below.在您的ThirdActivity_ColorPicker中,更改每个颜色按钮单击的代码,即(红色、绿色、蓝色等),如下所示。 This will set the Bundle data and result for the previous activity.这将为上一个活动设置捆绑数据和结果。

 Intent intentRed = new Intent();
 intentRed.putExtra(COLOR_CODE, 
 getApplicationContext().getResources().getColor(R.color.redBackground));
 setResult(Activity.RESULT_OK, intentRed);
 finish();

Then, in your MainActivity , in onActivityResult , make below changes:然后,在您的MainActivity中,在onActivityResult中,进行以下更改:

 if (requestCode == COLOR_PICKER_REQUEST && resultCode == RESULT_OK) {
   if (data != null && data.getExtras() != null) {
      Bundle bundle = data.getExtras();
      int colorBg = bundle.getInt(COLOR_CODE);
      findViewById(R.id.mainBg).setBackgroundColor(colorBg);
    }
 }

COLOR_PICKER_REQUEST is the int , which you set for the startActivityForResult and COLOR_CODE is any String for the bundle key. COLOR_PICKER_REQUEST是您为startActivityForResult设置的int ,而COLOR_CODE是捆绑键的任何String

暂无
暂无

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

相关问题 在第二个活动中单击一个按钮时,如何使用微调器更改主要活动的背景颜色? - 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 the background color of an activity from another activity? 如何在Java中仅一秒钟更改文本框的背景颜色? - How can I change the background color of my textbox for just a second in Java? 如何更改我的按钮颜色,使用 android:background 给它白色边框? - How do I change my Buttons color, using android:background gives it white borders? 如何从 Settings Activity 修改 MainActivity 上的数据? - How do i modify data on my MainActivity from Settings Activity? 如何更改按钮的颜色? - How can I change the color of buttons? 如何将Paint或Color传递给使用活动中的onDraw()方法的新对象? - How can I pass a Paint or Color to my new object that is using the onDraw() method from an activity? 如何在第二个活动的Texview中显示Mainactivity中的变量 - How to Display a variable from Mainactivity in a second Activity's Texview 如何使用 iText 7 为我的 PDF 添加背景色? - How can I add a background color to my PDF using iText 7? 通过共享首选项更改其他活动onCheckedChange上按钮的背景颜色(?) - Change background color of buttons from another activity onCheckedChange through shared preference(?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM