我试图以编程方式在kotlin中发送电子邮件。如何将字符串数组传递给android电子邮件意图
这是我的代码,我从我在网上找到的一个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开发者docs说Intent.EXTRA_EMAIL是“所有的字符串数组‘’收件人的电子邮件地址。”我应该如何定义addressees
变量?
编辑:我发现,如果我硬代码我的电子邮件地址,
val addressees = arrayOf("[email protected]")
然后应用程序工作正常。当我到达电子邮件应用程序时,我的地址位于“收件人:”字段中,我可以向自己发送邮件。但是,如果我在程序中输入了相同的地址,它就不会显示在电子邮件客户端中。而且,当我在调试器下看它时,意图从两种方式显示完全相同的值。 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>
你怎么了? Kotlin'arrayOf(...)'相当于Java'String []'。 –
@ holi-java那么为什么这不工作? – saulspatz
:),我不是一个android开发者,并且你没有给出任何信息,没有人知道为什么没有工作。它似乎是你的MIME类型是错误的,也许这可以帮助你:https://stackoverflow.com/questions/8701634/send-email-intent –