2017-07-30 47 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

相關問題