2017-07-30 49 views
1

我试图以编程方式在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> 
+0

你怎么了? Kotlin'arrayOf (...)'相当于Java'String []'。 –

+0

@ holi-java那么为什么这不工作? – saulspatz

+0

:),我不是一个android开发者,并且你没有给出任何信息,没有人知道为什么没有工作。它似乎是你的MIME类型是错误的,也许这可以帮助你:https://stackoverflow.com/questions/8701634/send-email-intent –

回答

2

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

编辑

根据您的最后一次更新,to没有被识别为String。下面的代码工作...

  val to = "[email protected]" 
      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="[email protected]" 
    android:id="@+id/editTextTo"/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="BUTTON" 
    android:id="@+id/button"/> 
</LinearLayout> 

正如你所看到的,差异是微不足道的,但是这个工作,而您没有。我怀疑问题出在你的XML文件中。你可以发布吗?

+0

对不起,我的意思是'intent.setType(“*/*”)' – Les

+0

:),您可以使用反斜杠'\\'来转义降价符号。 –

+0

“TO:”字段仍空白。我将不得不在其他设备上尝试它,但我现在还没有一个可用的设备。 – saulspatz

0

我尝试了Les的使用arrayOf(to.toString())的建议,但android studio灰显了.toString(),表示它已经知道它是一个字符串了。然后我试图更明确:

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

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

+0

我上次编辑使用相同的构造,而Kotlin没有干扰类型的问题。 – Les

相关问题