如何将字符串数组传递给android电子邮件意图

我试图以编程方式发送电子邮件在kotlin。

这是我的代码,我从我在网上找到的一个java例子翻译过来。

package com.example.emaildemo import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; class MainActivity: AppCompatActivity() { override fun onCreate(savedInstanceState:Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val editTextTo:EditText = findViewById(R.id.editText) val editTextSubject:EditText = findViewById(R.id.editText2) val editTextMessage:EditText = findViewById(R.id.editText3) val button1:Button = findViewById(R.id.button) button1.setOnClickListener(View.OnClickListener{ val to = editTextTo.getText().toString() val subject = editTextSubject.getText().toString() val message = editTextMessage.getText().toString() val intent = Intent(Intent.ACTION_SEND) val addressees = arrayOf(to) intent.putExtra(Intent.EXTRA_EMAIL, addressees) intent.putExtra(Intent.EXTRA_SUBJECT, subject) intent.putExtra(Intent.EXTRA_TEXT, message) intent.setType("message/rfc822") startActivity(Intent.createChooser(intent, "Select Email Sending App :")) }) } } 

它似乎正常工作,除了“TO”字段不复制到用户的电子邮件应用程序。 java代码说:

 intent.putExtra(Intent.EXTRA_EMAIL, new String[]{TO}); 

和android开发人员文档说,Intent.EXTRA_EMAIL是“所有的字符串数组”到“收件人的电子邮件地址”。 我应该如何定义addressees变量?

编辑:我发现,如果我硬编码我的电子邮件地址,

 val addressees = arrayOf("me@email.com") 

那么该应用程序按预期工作。 当我到达电子邮件应用程序,我的地址是在“收件人:”字段,我可以发送邮件给自己。 但是,如果我在程序中键入相同的地址,则不会显示在电子邮件客户端中。 而且,当我在调试器下面看时,意图显示了两种方式完全相同的值。 Curiouser和curiouser。

EDIT2

这是我的XML文件。 (不知何故,我不能得到前两行正确缩进。)

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18dp" android:text="To" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18dp" android:text="Subject" android:id="@+id/textView2" android:layout_below="@+id/editText" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18dp" android:text="Message" android:id="@+id/textView3" android:layout_below="@+id/editText2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textMultiLine" android:lines="4" android:id="@+id/editText3" android:layout_below="@+id/textView3" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Here To Send Email from android application programmatically" android:id="@+id/button" android:layout_below="@+id/editText3" android:layout_centerHorizontal="true" android:layout_marginTop="44dp" /> </RelativeLayout> 

您的TO语句看起来是正确的,我不认为你需要明确(即使用arrayOf<String>(to) )。

编辑

根据您的上次更新,不被识别为String 。 下面的代码工程…

  val to = "person@gmail.com" val subject = "Test" val message = "Test" val intent = Intent(Intent.ACTION_SEND) val addressees = arrayOf(to) intent.putExtra(Intent.EXTRA_EMAIL, addressees) intent.putExtra(Intent.EXTRA_SUBJECT, subject) intent.putExtra(Intent.EXTRA_TEXT, message) intent.setType("message/rfc822") startActivity(Intent.createChooser(intent, "Send Email using:")); 

editTextTo.getText().toString()的调用正在返回一个构建器或者一个真正的String以外的东西。 而是尝试调用arrayOf(to.toString())

EDIT2

以下是一个可用的代码示例,即填写电子邮件应用程序中的“收件人”字段。

 package com.x.edittextexp import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button import android.widget.EditText class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val button: Button = findViewById(R.id.button) val editTextTo: EditText = findViewById(R.id.editTextTo) button.setOnClickListener(View.OnClickListener { val to = editTextTo.getText().toString() val subject = "Test" val message = "Test" val intent = Intent(Intent.ACTION_SEND) val addressees = arrayOf(to) intent.putExtra(Intent.EXTRA_EMAIL, addressees) intent.putExtra(Intent.EXTRA_SUBJECT, subject) intent.putExtra(Intent.EXTRA_TEXT, message) intent.setType("message/rfc822") startActivity(Intent.createChooser(intent, "Send Email using:")); }) } } 

和XML看起来像这样

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:hint="person@gmail.com" android:id="@+id/editTextTo"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="BUTTON" android:id="@+id/button"/> </LinearLayout> 

正如你所看到的,差异是微不足道的,但这是有效的,而你的则不然。 我会怀疑问题是在你的XML文件。 你可以发布吗?

我尝试了Les的使用arrayOf(to.toString())的建议,但android工作室变灰.toString()表明它知道这是一个字符串已经。 然后我试着更加明确:

 val addressees: Array<String> = arrayOf(to) 

它的工作! 我根本不明白这一点。 类型推断似乎kotlin应该没有任何麻烦。 也许有些上师会看到这个并解释它; 我会感兴趣和感激。