试图从另一个活动(Kotlin/Android Studio)中的代码更改一个活动中的背景色

试图从另一个活动(Kotlin/Android Studio)中的代码更改一个活动中的背景色,android,android-activity,kotlin,radio-button,Android,Android Activity,Kotlin,Radio Button,我正在Android Studio 3.1.3中用Kotlin编程。我试图创建一个活动,通过单击按钮将其背景颜色更改为16种颜色中的任意一种。这个应用程序是为我7岁的侄女设计的,所以更简单的界面会更好 我的方法是使用一个按钮来启动第二个活动,其中包含选择颜色的单选按钮。选择颜色后,我想返回到第一个活动并自动更改约束布局背景的颜色。我可以通过使用第二个按钮来实现这一点,但没有成功地使用单个按钮(首选方法) 下面是这一点上主要活动的非常简单的代码,包括两个按钮调用的函数,这两个按钮使这项工作正常。使

我正在Android Studio 3.1.3中用Kotlin编程。我试图创建一个活动,通过单击按钮将其背景颜色更改为16种颜色中的任意一种。这个应用程序是为我7岁的侄女设计的,所以更简单的界面会更好

我的方法是使用一个按钮来启动第二个活动,其中包含选择颜色的单选按钮。选择颜色后,我想返回到第一个活动并自动更改约束布局背景的颜色。我可以通过使用第二个按钮来实现这一点,但没有成功地使用单个按钮(首选方法)

下面是这一点上主要活动的非常简单的代码,包括两个按钮调用的函数,这两个按钮使这项工作正常。使用xml中按钮的onClick事件调用函数(如下所示)

请注意,“var color…”语句位于MainActivity类之外,因此可以跨项目中的所有活动访问它。它在第二个活动中更改,我想使用该结果更改第一个活动中的背景色

var color\u Background:Int=color\u White

类MainActivity:AppCompatActivity(){

}

上面的两个函数由按钮的xml onClick事件调用,如下所示

<Button
    android:id="@+id/btn_SetColor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="16dp"
    android:onClick="setColor_click"
    android:text="@string/set_color"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
    <RadioButton
        android:id="@+id/rad_Red"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="Red_clicked"
        android:text="Red"
        android:buttonTint="@color/White"
        android:textStyle="bold" />
同样,每个“color”函数都由一个XMLOnClick事件为其相应的单选按钮调用,如下所示

<Button
    android:id="@+id/btn_SetColor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="16dp"
    android:onClick="setColor_click"
    android:text="@string/set_color"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
    <RadioButton
        android:id="@+id/rad_Red"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="Red_clicked"
        android:text="Red"
        android:buttonTint="@color/White"
        android:textStyle="bold" />
我阅读了有关活动生命周期的内容,这就是为什么我尝试了各种“开启”方法。我还尝试了一些编程选项的组合,但我唯一能让它工作的方法是使用两个按钮


任何关于如何使用单个按钮将Main活动中的背景色设置更改为16种颜色之一的想法都将不胜感激。谢谢你抽出时间

您可能想了解的是
startActivityForResult
。您可以传递一个
int
,您的
活动返回结果时会收到通知

// Define some constants, the request code and the place we're passing the result.
companion object {
    private const val COLOR_REQUEST= 10001
    const val EXTRA_COLOR = "EXTRA_COLOR"
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
}

// Now, call startActivityForResult instead, passing the request code.

fun setColor_click (view: View) {
    val intent = Intent (this, ChangeColor::class.java)
    startActivityForResult(intent, COLOR_REQUEST)
}

fun changeColor_click (view: View) {
    val cl: ConstraintLayout = findViewById(R.id.constraint_Layout)
    cl.setBackgroundColor(color_Background)
}

// And now you can handle the result.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if( requestCode == COLOR_RESULT && resultCode == Activity.RESULT_OK && data != null  ) {
        val color = data.extras.getInt(EXTRA_COLOR)
        // TODO use the color.
    }
    super.onActivityResult(requestCode, resultCode, data)
}
现在,在您的
ChangeColor
活动中,您需要确保在调用
finish
时返回颜色

fun Red_clicked(view: View) {
    val intent = Intent()
    intent.putExtra(MainActivity.EXTRA_COLOR, color_Red)
    setResult(Activity.RESULT_OK, intent)
    finish()
    return
}

就这样。你会想改变它,也许会通过一些更容易的。但是颜色的
id
现在应该可以使用了。

经过进一步的研究和实验,我发现了以下额外的解决方案

在主活动(类MainActivity)中添加以下onStart函数。这显然是在a)应用程序第一次启动,以及b)应用程序将控制权从第二个活动转移回第一个活动时执行的

override fun onStart() {
        super.onStart()  
        val cl: ConstraintLayout = findViewById(R.id.constraint_Layout)
        cl.setBackgroundColor(color_Background)
}
override fun onStart() {
        super.onStart()  
        val cl: ConstraintLayout = findViewById(R.id.constraint_Layout)
        cl.setBackgroundColor(color_Background)
}