我正在尝试将用户的输入发送到第二个屏幕。值为dgcoutMEA等。以下是主要 Activity 的代码,带有将您发送到第二屏幕的按钮:

package com.example.prodapp12

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.sulfur


class MainActivity : AppCompatActivity() {

    companion object {
        const val dgc = "dgc" // key for Your value, You have to define each key for every value that You want to pass to another activity
        const val dcx = "dcx"
        const val outmea = "outmea"
        const val outdea = "outdea"
        const val sulfur = "sulfur"
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val dgc = dgc
        val dcx = dcx
        val outmea = outmea
        val outdea = outdea
        val sulfur = sulfur



class ResultActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rezultate)

        val dcx = intent.getStringExtra(MainActivity.dcx) // receive saved value
        Log.d("dcx", "Your value: dcx") // D/MyTag: Your value: My string
        val dgc = intent.getStringExtra(MainActivity.dgc)
        Log.d("dgc", "Your value: dgc")
        val outmea = intent.getStringExtra(MainActivity.outmea)
        Log.d("outmea", "Your value: outmea")
        val outdea = intent.getStringExtra(MainActivity.outdea)
        Log.d("outdea", "Your value: outdea")
        val sulfur = intent.getStringExtra(MainActivity.sulfur)
        Log.d("sulfur", "Your value: sulfur")

    }
}
        calc.setOnClickListener {
            val intent = Intent(this, ResultActivity::class.java).apply {
                putExtra("dcx", "dcx") // add value to intent with defined key
                putExtra("dgc", "dgc") // add value to intent with defined key
                putExtra("outmea", "outmea") // add value to intent with defined key
                putExtra("outdea", "outdea") // add value to intent with defined key
                putExtra("sulfur", "sulfur") // add value to intent with defined key
            }
            startActivity(Intent(this, Rezultate::class.java))

        }
    }
}
inMEA是一个textView,需要使用第一个屏幕中的输入值来显示一些简单的算术计算
这是第二个屏幕:
包com.example.prodapp12
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_rezultate.*




class Rezultate : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rezultate)

        val extras = intent.extras
        if (extras != null) {
            val dcx = extras.getString("dcx")
            val dgc = extras.getString("dgc")
            val outmea = extras.getString("outmea")
            val outdea = extras.getString("outdea")
            val sulfur = extras.getString("sulfur")
            //The key argument here must match that used in the other activity
        }


        Ref.setOnClickListener {
            inMEA.text = intent.getStringExtra(MainActivity.sulfur)

        }
    }
}

最佳答案

这是您如何做到的示例:
主要 Activity :

class MainActivity : AppCompatActivity() {

    companion object {
        const val YOUR_VALUE_KEY = "YOUR_VALUE_KEY" // key for Your value, You have to define each key for every value that You want to pass to another activity
    }

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

        val yourValue = "My string"

        // create listenner for button
        buttonGoToResultActivity.setOnClickListener {
            val intent = Intent(this, ResultActivity::class.java).apply {
                putExtra(YOUR_VALUE_KEY, yourValue) // add value to intent with defined key
            }
            startActivity(intent) // start activity
        }

    }
}
ResultActivity:
class ResultActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_result)

        val receivedYourValue = intent.getStringExtra(MainActivity.YOUR_VALUE_KEY) // receive saved value
        Log.d("MyTag", "Your value: $receivedYourValue") // D/MyTag: Your value: My string

    }
}

主要 Activity :
class MainActivity : AppCompatActivity() {

    companion object {
        const val DGC_KEY = "DGC_KEY"
        const val DCX_KEY = "DCX_KEY"
        const val OUTMEA_KEY = "OUTMEA_KEY"
        const val OUTDEA_KEY = "OUTDEA_KEY"
        const val SULFUR_KEY = "SULFUR_KEY"
    }

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

        val dgc = "dgc_value"
        val dcx = "dcx_value"
        val outmea = "outmea_value"
        val outdea = "outdea_value"
        val sulfur = "sulfur_value"

        calc.setOnClickListener {
            val intent = Intent(this, ResultActivity::class.java).apply {
                putExtra(DGC_KEY, dgc)
                putExtra(DCX_KEY, dcx)
                putExtra(OUTMEA_KEY, outmea)
                putExtra(OUTDEA_KEY, outdea)
                putExtra(SULFUR_KEY, sulfur)
            }
            startActivity(intent)
        }
    }
}
ResultActivity:
class ResultActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_result)

        val dgc = intent.getStringExtra(MainActivity.DGC_KEY)
        val dcx = intent.getStringExtra(MainActivity.DCX_KEY)
        val outmea = intent.getStringExtra(MainActivity.OUTMEA_KEY)
        val outdea = intent.getStringExtra(MainActivity.OUTDEA_KEY)
        val sulfur = intent.getStringExtra(MainActivity.SULFUR_KEY)

        Log.d("MyTag", "dgc = $dgc")
        Log.d("MyTag", "dcx = $dcx")
        Log.d("MyTag", "outmea = $outmea")
        Log.d("MyTag", "outdea = $outdea")
        Log.d("MyTag", "sulfur = $sulfur")
    }
}
日志:
D/MyTag: dgc = dgc_value
D/MyTag: dcx = dcx_value
D/MyTag: outmea = outmea_value
D/MyTag: outdea = outdea_value
D/MyTag: sulfur = sulfur_value

关于android - 如何使用Kotlin在Android Studio中从一项 Activity 获取值(value)到另一项 Activity ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64048514/

10-12 01:50