简体   繁体   English

如何在Android应用程序中将数据从一项活动传递到另一项活动

[英]How to pass data from one activity to another in Android application

I'm trying to create simple app where you type your data in one activity by editText and it's shown in another in TextView. 我正在尝试创建一个简单的应用程序,您可以在其中通过editText在一个活动中键入数据,并在TextView的另一个活动中显示该数据。 But i really don't know how to make it work. 但是我真的不知道如何使它工作。 As you can see this is simple code but right now when i click submit nothing happens on another activity. 如您所见,这是简单的代码,但是现在当我单击“提交”时,其他活动没有任何反应。 this is first activty 这是第一个活动

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    fun switchToActivity (view: View){
        val myIntent = Intent(this, Main2Activity::class.java)
        startActivity(myIntent)
    }
    override fun onActivityResult(requestCode: Int,
                                  resultCode: Int,
                                  myInten: Intent?) {
        var text2 = myInten?.getStringExtra("text")
        textView.setText(text2)
    }
}

And this is second 这是第二

class Main2Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
    }
    fun buttonListener (view: View){
        var text = editText.text.toString()

        val myIntent = Intent()
        myIntent.putExtra("textView", text)
        setResult(Activity.RESULT_OK, myIntent)
        finish()
    }
}

There are 2 issues with your code. 您的代码有2个问题。

First, to receive result from another activity, you must use startActivityForResult() instead. 首先,要从另一个活动中接收结果,必须改为使用startActivityForResult()

fun switchToActivity (view: View){
    val myIntent = Intent(this, Main2Activity::class.java)
    startActivityForResult(myIntent)
}

Second, you must use the same key when saving and retrieving result. 其次,保存和检索结果时必须使用相同的密钥。

var text2 = myInten?.getStringExtra("textView")

Put it together. 把它放在一起。

class MainActivity : AppCompatActivity() {

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

    fun switchToActivity (view: View){
        val myIntent = Intent(this, Main2Activity::class.java)
        startActivityForResult(myIntent)
    }

    override fun onActivityResult(requestCode: Int,
                                  resultCode: Int,
                                  myInten: Intent?) {
        var text2 = myInten?.getStringExtra("textView")
        textView.setText(text2)
    }
}

您应该使用: startActivityForResult(myIntent)而不是: startActivity(myIntent)

暂无
暂无

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

相关问题 如何将数据从一个活动传递到 Android 中的另一个活动 - How to pass data from one activity to another activity in Android MVP Android:如何将数据从一个活动传递到另一个活动? - MVP Android: how to pass data from one activity to another? 如何将数据从活动传递到另一个活动,然后再传递到Android上的另一个活动? - How to pass data from an activity to another activity and then to another activity on Android? 如何从一个活动的列表视图传递数据并将其传递给另一活动? - How to pass the data from listview of one activity and pass it to another activity? 如何使用Application类将数据从一个活动传递到另一个活动 - how to pass data from one activity to another activity with th use of Application class 如何将数据从一个Android应用程序传递到另一个? - how to pass a data from one android application to another? 如何将数组从一个活动传递到android中的另一个活动 - how to pass an array from one activity to another activity in android Android:如何将WebView从一个活动传递到另一个活动 - Android: How to Pass WebView from one Activity to another Activity 如何将多个记录从一个活动传递到android中的另一个活动? - How to pass multiple records from one activity to another activity in android? 如何在Android中将两个字符串从一个活动传递到另一个活动 - How to pass two Strings from one Activity to another Activity in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM