我有两个文本框:一个用于主题,另一个用于实际消息。不幸的是,当我在运行 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"));
}
};