android - 为什么在活动之间发送数据不起作用?
问题描述
我目前正在使用 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 岁男孩,我想成为一名程序员并学习
解决方案
您需要使用 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
推荐阅读
- sql - 如何处理从多个请求生成自定义复合键
- google-apps-script - 在输入特定单词时寻找弹出窗口 google sheet
- python - 使用 python 将 C 代码拆分为语句
- wordpress - Wordpress:通过小部件创建自定义页面
- sabre - BargainFinderMaxRQ 隔日响应无可用性错误
- rest - 使用服务帐户和 oauth2 调用 autoML Google API
- forms - TYPO3 8.7 EXT:form - 在刷新页面之前不会在整理器中显示闪烁消息
- windows - 在 Windows 上使用带有 Code::Blocks 的 SDL-C 库...向导找不到 SDLmain 库文件
- ios - 在 Swift 中的 TableViewCell 中居中 iCarousel
- javascript - JavaScript 中的小数舍入