【问题标题】:Pass info from edittext through an email intent通过电子邮件意图传递来自edittext的信息
【发布时间】:2013-03-23 04:57:46
【问题描述】:

我有两个文本框:一个用于主题,另一个用于实际消息。不幸的是,当我在运行 android 4.2.2 和最新 gmail 应用程序的手机上测试时,我使用的代码没有传递任何信息。

更新方法。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.tipus);
findViewById(R.id.sendemailbutton).setOnClickListener(emailclick);
findViewById(R.id.cancelbtn).setOnClickListener(myclick);
subjectline = (EditText) findViewById(R.layout.tipus);
emailcontent = (EditText) findViewById(R.layout.tipus);

}

public final Button.OnClickListener emailclick = new Button.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);  

        String aEmailList[] = { oliuremail, vikemail };   
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);  //supposed to pass info of the emails to the email app
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subjectline.getText().toString());  //not taking stuff from an edittext, only things in quotes. 
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailcontent.getText().toString());  //won't accept anything from an edittext.
        emailIntent.setType("plain/text");
        startActivity(Intent.createChooser(emailIntent, "Send Email Using...")); 
    }

};

tipus.xml(更新)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/messagetitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/emailmessage"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="46dp"
    android:text="Message:" />

<EditText
    android:id="@+id/emailmessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="122dp"
    android:ems="10"
    android:hint="Write your message here."
    android:inputType="textMultiLine" />

<Button
    android:id="@+id/cancelbtn"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/arrowrightblack" />

<Button
    android:id="@+id/sendemailbutton"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Send" />

<EditText
    android:id="@+id/subjectline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/subjectdenoter"
    android:layout_alignBottom="@+id/subjectdenoter"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:hint="Please Add a Subject." />

<TextView
    android:id="@+id/subjecttitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignRight="@+id/textView3"
    android:layout_marginBottom="82dp"
    android:text="Subject:" />

当我使用此代码时,信息会传递到电子邮件应用程序。但是,我有两个文本框,用户将在其中输入主题,然后输入电子邮件的实际文本。我需要从两个文本框中传递的信息发送到电子邮件应用程序。我查看了 stackoverflow 和 tutsplus,他们有一些信息,但是那些谈论从文本框中传递数据的信息不是很丰富。提前致谢。

编辑:

我终于找到了答案。您必须创建一个私有字符串,例如:

private String getSubjectContent() {
    String text = "";

    text += yourtexteditname.getText().toString() + "\n";

    return text;
}

注意:您可以在类中创建尽可能多的这些方法。在每种方法中,只需将“yourtexteditname”替换为 textEdit 的名称即可。如果您想从多个 textEdits 获取信息并将它们放在电子邮件中的一个位置(主题行、消息正文等),您还可以将多个 textEdits 放入一个方法中。

然后,您所要做的就是添加普通代码来启动意图。像这样:

public final Button.OnClickListener emailclick = new Button.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        variablename = (EditText) findViewById(R.id.textviewname); //defining the textedit you want to get info from. 
        variablename = (EditText) findViewById(R.id.othertextviewname); //defining the textEdit you want to get info from.
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("message/rfc822"); //specifies message for email app.
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, getEmailContent()); //adds the actual content of the email by calling the method previously defined
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getSubjectContent()); //adds the subject by calling the method previously defined.
        startActivity(Intent.createChooser(emailIntent, "Title of the dialog chooser"));


    }

};

【问题讨论】:

  • 你试过EditText.getText吗?
  • 你必须做什么?您需要从电子邮件信息中使用的用户那里获取价值,作为 extra_email 和 extra_text。
  • 您能否展示您的编辑文本在哪里以及如何从这些编辑文本中检索数据以放入意图?
  • @adneal 是的,我已经尝试过了,它不起作用并导致我的应用程序崩溃。
  • @ThatGuy您能否在不修改任何内容的情况下使用“tipus.xml”内容更新此问题。

标签: android email android-intent android-edittext


【解决方案1】:

您可以通过使用字符串变量来使用它。 喜欢..

String subjectdata = edittext.getText().toString();
String sharedata = edittext1.getText().toString();

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_SUBJECT, subjectdata);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc@gmail.com"});
email.putExtra(Intent.EXTRA_TEXT, shareddata);
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Send Mail :"));

【讨论】:

    【解决方案2】:

    由于您要发送给多个电子邮件收件人,我认为您需要使用Intent.ACTION_SEND_MULTIPLE而不是Intent.ACTION_SEND

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    

    一些实现也更喜欢Intent.ACTION_SENDTO

    (可选)你可能想使用这个:

    emailIntent.setType("message/rfc822");
    

    而不是这个:

    emailIntent.setType("plain/text");
    

    最后,从EditText 字段中检索用户的文本条目,如下所示:

    String <body-or-subject-text> = editText.getText().toString();
    

    【讨论】:

    • 很抱歉问了这么多问题,但我对 android 开发还比较陌生,想知道应该用什么来代替“” ?另外,我发现有时列出意图的顺序很重要。你知道我应该列出它们的顺序吗?我尝试了 Android_User 的答案,但它也强行关闭了我(我修改了它并使用了 ACTION_SEND_MULTIPLE)。谢谢。
    • 是的。您必须替换一个字符串名称,如下所示:“String myText = editText.getText().toString();”。这是您从“EditText”小部件获取用户编辑文本的方式,以便您可以将其发送到电子邮件中。
    • 这仍然使我的应用程序崩溃,我不知道为什么。这是查看我的代码的链接。 pastebin.com/BFiNuBQi
    • 您将“subjectdata”和“emailmsg”都放在“EXTRA_SUBJECT”上。 “emailmsg”应该继续“EXTRA_TEXT”。此外,如果可能,从 'startActivity()' 切换到 'startActivityForResult()' (需要额外的 int 'resultCode' 参数),以便在用户发送消息后执行将返回到 Activity 的 'onActivityResult()' 函数。不幸的是,我认为这些更新都不会解决您的崩溃问题。为此,请检查 LogCat 中的崩溃行号。或者,发布 LogCat 输出(此处或 PasteBoard)。
    • 好的,你应该把'new Intent'行放回原来的样子:“Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);”,也试试“ACTION_SENDTO”。
    猜你喜欢
    • 1970-01-01
    • 2013-02-21
    • 2012-05-26
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 2022-01-04
    • 2020-09-14
    • 1970-01-01
    相关资源
    最近更新 更多