首页 > 解决方案 > 为什么在活动之间发送数据不起作用?

问题描述

我目前正在使用 Kotlin。我正在尝试通过转到另一个活动即颜色设置活动来更改主要活动中框的颜色。我的代码没有返回错误但无法正常工作。我尝试在此页面上阅读不同的结果,但没有人回答我的问题。谢谢你的帮助。

主要活动.kt

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

        val button_box = findViewById<Button>(R.id.button_box)

        button_box.setOnClickListener {
            val intent = Intent(this, boxColor::class.java)
            startActivity(intent)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

            if (resultCode == RESULT_OK) {

                val returnColor = data!!.getStringExtra("colorName")

                val boxColorChoice = when (returnColor) {
                    "green" -> R.drawable.box_green
                    "grey" -> R.drawable.box_grey
                    "lblue" -> R.drawable.box_lblue
                    "purple" -> R.drawable.box_purple
                    "red" -> R.drawable.box_red
                    "white" -> R.drawable.box_white
                    "yellow" -> R.drawable.box_yellow
                    else -> R.drawable.box_white
                }
                button_box.setBackgroundResource(boxColorChoice)
            }
        }

这是 boxcolor.kt

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

        val green = findViewById<Button>(R.id.green_box)
        val red = findViewById<Button>(R.id.red_box)
        val white = findViewById<Button>(R.id.white_box)
        val yellow = findViewById<Button>(R.id.yellow_box)
        val grey = findViewById<Button>(R.id.grey_box)
        val lblue = findViewById<Button>(R.id.lblue_box)

        green.setOnClickListener {
            val intent = Intent()
            intent.putExtra("colorName", "green")
            setResult(Activity.RESULT_OK, intent)
            finish()
        }

     (and the same onclicklistener for all the other colours)

我也知道这是一个小问题,但非常感谢您的帮助。我是来自拉贾斯坦邦的 14 岁男孩,我想成为一名程序员并学习

标签: androidkotlin

解决方案


您需要使用 startActivityForResult 您的代码必须更改为:

主要活动.kt

...
val mRequestCode = 101  //ADD THIS LINE
...
button_box.setOnClickListener {
            val intent = Intent(this, boxColor::class.java)
            startActivityForResult(intent, mRequestCode) //ADD THIS LINE
        }
...
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

            if (resultCode == mRequestCode) { //CHANGE THIS LINE
                ...
            }
        }

在你的 boxcolor.kt 中:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_box_color)
        val mRequestCode = 101  //ADD THIS LINE

        ...

        green.setOnClickListener {
            val intent = Intent()
            intent.putExtra("colorName", "green")
            setResult(mRequestCode, intent)  //CHANGE THIS LINE
            finish()
        }

     (and the same onclicklistener for all the other colours)

注意不要使用 Activity.RESULT_OK 作为 requestCode


推荐阅读