如何添加另一个字符串以及如何检索它.我尝试在配套对象中添加另一个常量,但不起作用


class NewWordActivity : AppCompatActivity() {
private lateinit var editWordView: EditText
private lateinit var editTitleView: EditText

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new_word)
editWordView = findViewById(R.id.edit_word)
editTitleView = findViewById(R.id.title_word)

val button = findViewById<Button>(R.id.button_save)
button.setOnClickListener {
val replyIntent = Intent()
if ((TextUtils.isEmpty(editWordView.text))) {
setResult(Activity.RESULT_CANCELED, replyIntent)
Toast.makeText(this, "Can not save empty fields", Toast.LENGTH_SHORT).show()
} else {
val word = editWordView.text.toString()
val title = editWordView.text.toString()
replyIntent.putExtra(EXTRA_REPLY, word)
//How can I add title I have tried adding another constant but not working
replyIntent.putExtra(EXTRA_SEND, title)
setResult(Activity.RESULT_OK, replyIntent)
}
finish()
}
}
companion object {
const val EXTRA_REPLY = "com.example.android.wordlistsql.REPLY"
//I addded this but still not working
const val EXTRA_SEND = "com.example.android.wordlistsql.REPLY"
}
}

I have tried passing data between the two actitvities but while doing that whenever I call the startActivity(intent) nothing is passed

我相信就像更改一样简单:-

companion object {
const val EXTRA_REPLY = "com.example.android.wordlistsql.REPLY"
//I addded this but still not working
const val EXTRA_SEND = "com.example.android.wordlistsql.REPLY"
}

自:-

companion object {
const val EXTRA_REPLY = "com.example.android.wordlistsql.REPLY"
//I addded this but still not working
const val EXTRA_SEND = "com.example.android.wordlistsql.SEND" //<<<<< made value unqiue
}

这是EXTRA_REPLY和EXTRA_SEND相同的值,因此当用作意图中的键时,它只是一个额外的意图。它们需要是唯一的值来区分不同的 EXTRA。

最新更新