Kotlin putExtra可以使用提供的参数调用以下函数

时间:2018-04-23 13:56:21

标签: android android-studio kotlin

向Kotlin完成新手,试图让三个值从文本框传递到另一个页面,即时通讯没有错误放置信息也没有被传递或显示我已经尝试了几个小时让这个工作无济于事

请有人指出我的方向是好的还是正确的。

我现在已经编辑了我的代码,如下面的帮助中所示,该页面现在正在接收我无法显示的信息。

PAGE1

package com.example.james.visitorapp

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.AlarmClock.EXTRA_MESSAGE
import android.view.View
import android.widget.EditText

class MainActivity : AppCompatActivity() {

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

    /** Called when the user taps the Send button */
    fun sendMessage(view: View) {
        val editText = findViewById<EditText>(R.id.editText)
        val editText2 = findViewById<EditText>(R.id.editText2)
        val editText3 = findViewById<EditText>(R.id.editText3)
        val message1 = editText.text.toString()
        val message2 = editText2.text.toString()
        val message3 = editText3.text.toString()


        val intent = Intent(this, DisplayMessageActivity::class.java).apply {
            putExtra(EXTRA_MESSAGE, arrayOf(message1, message2, message3))
        }

        startActivity(intent)
    }
}

receiving page

package com.example.james.visitorapp

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.AlarmClock.EXTRA_MESSAGE
import android.widget.TextView

class DisplayMessageActivity : AppCompatActivity() {

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

        // Get the Intent that started this activity and extract the string
        val message1 = intent.getStringExtra(EXTRA_MESSAGE)
        val message2 = intent.getStringExtra(EXTRA_MESSAGE)
        val message3 = intent.getStringExtra(EXTRA_MESSAGE)
        // Capture the layout's TextView and set the string as its text
        val textView = findViewById<TextView>(R.id.textView).apply {
            text = message1

        }
        val textView2 = findViewById<TextView>(R.id.textView2).apply {
            text = message2

        }
        val textView3 = findViewById<TextView>(R.id.textView3).apply {
            text = message3

        }
    }
}

2 个答案:

答案 0 :(得分:0)

请尝试这个

val intent = Intent(this, DisplayMessageActivity::class.java)
intent.putExtra(EXTRA_MESSAGE_1, message1)
intent.putExtra(EXTRA_MESSAGE_2, message2)
intent.putExtra(EXTRA_MESSAGE_3, message3)

答案 1 :(得分:0)

putExtra()没有任何期望vararg的重载。

您可以在Array

中发送消息
putExtra(EXTRA_MESSAGES, arrayOf(message1, message2, message3))

并在接收getStringArrayExtra()中使用Activity获取它们,例如:

// the inferred type of messages will be Array<String>
val messages = intent.getStringArrayExtra(EXTRA_MESSAGES)
// joining the messages into a single String and setting it as the text for the TextView
textView.text = messages.joinToString(separator = " ")

或者用不同的键分别放置它们:

putExtra(EXTRA_MESSAGE_1, message1)
putExtra(EXTRA_MESSAGE_2, message2)
putExtra(EXTRA_MESSAGE_3, message3)

(然后逐一获得getStringExtra()

在旁注中,此处调用apply()是多余的(在这两种情况下),您可以直接在putExtra()实例上调用Intent

val intent = Intent(this, DisplayMessageActivity::class.java)
    .putExtra(EXTRA_MESSAGE_1, message1)
    .putExtra(EXTRA_MESSAGE_2, message2)
    .putExtra(EXTRA_MESSAGE_3, message3)