1

我正在尝试通过电子邮件发送多个 Edittext 字段。

该代码将更能理解我想要完成的工作,

public class EmailSupport extends Activity {

  Button buttonSend;
  String textTo;
  EditText textSubject;
  EditText nametext;
  EditText emailtext;
  EditText phonetext;
  EditText topictext;
  EditText detailstext;


@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.email_form_to_support);

        public void onClick(View v) {
        String[] recipients = new String[]{"email@email.com", "email@email.com",};
          String subject = textSubject.getText().toString();
          String name = nametext.getText().toString();
          String emails = emailtext.getText().toString();
          String phone = phonetext.getText().toString();
          String topic = topictext.getText().toString();
          String details = detailstext.getText().toString();


          Intent email = new Intent(Intent.ACTION_SEND);
          email.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
          email.putExtra(Intent.EXTRA_SUBJECT, subject);
          email.putExtra(Intent.EXTRA_TEXT, name);
          email.putExtra(Intent.EXTRA_TEXT, emails);
          email.putExtra(Intent.EXTRA_TEXT, phone);
          email.putExtra(Intent.EXTRA_TEXT, topic);
          email.putExtra(Intent.EXTRA_TEXT, details);

我需要知道如何合并所有编辑文本输入,如下面的布局文件中所示,以基本添加到电子邮件中的消息文本。我的主题 edittext 是我的电子邮件主题,我想强制电子邮件地址默认发送它。

email_form_to_support.xml

  <?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/name" />


    <EditText
        android:id="@+id/nametext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/email" />



    <EditText
        android:id="@+id/emailtext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:minLines="1"
        android:scrollbars="vertical" />

<TextView
    android:id="@+id/phone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/phone" />


    <EditText
        android:id="@+id/phonetext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:maxLines="8"
        android:minLines="1"
        android:scrollbars="vertical" />

    <TextView
        android:id="@+id/topic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/topic" />


    <EditText
        android:id="@+id/topictext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:maxLines="3"
        android:minLines="1"
        android:scrollbars="vertical" />

    <TextView
        android:id="@+id/details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/details" />


    <EditText
        android:id="@+id/detailstext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:maxLines="8"
        android:minLines="1"
        android:scrollbars="vertical" />


    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/submit" />

</LinearLayout>

任何帮助将不胜感激。我觉得我只是错过了一些简单的东西,但我可能错了。到目前为止,使用这种来源方法,我在 String message = xxxtext.getText().toString(); 上遇到错误。因为我有好几次。我不知道该给每个人放什么。

Per Pavel Dudka 建议我将 Source 更改为 This

 public class EmailSupport extends Activity {

Button buttonSend;
String textTo;
EditText textSubject;
EditText nametext;
EditText emailtext;
EditText phonetext;
EditText topictext;
EditText detailstext;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.email_form_to_support);

    buttonSend = (Button) findViewById(R.id.submit);
    textTo = (String) getString(R.string.to);
    textSubject = (EditText) findViewById(R.id.topictext);
    nametext = (EditText) findViewById(R.id.nametext);
    emailtext = (EditText) findViewById(R.id.emailtext);
    phonetext = (EditText) findViewById(R.id.phonetext);
    topictext = (EditText) findViewById(R.id.topictext);
    detailstext = (EditText) findViewById(R.id.detailstext);

    buttonSend.setOnClickListener(new OnClickListener() {



        public void onClick(View v) {

            StringBuilder emailBodyBuilder = new StringBuilder();

            emailBodyBuilder.append(textSubject.getText().toString());
            emailBodyBuilder.append("\n");
            emailBodyBuilder.append(nametext.getText().toString());
            emailBodyBuilder.append("\n");
            emailBodyBuilder.append(emailtext.getText().toString());
            emailBodyBuilder.append("\n");
            emailBodyBuilder.append(topictext.getText().toString());
            emailBodyBuilder.append("\n");
            emailBodyBuilder.append(phonetext.getText().toString());
            emailBodyBuilder.append("\n");
            emailBodyBuilder.append(detailstext.getText().toString());

            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("plain/text");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new  String[]{"email1@domen.com", "email2@domen.com"});
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, textSubject.getText().toString());
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailBodyBuilder.toString());
            startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        }
    });
}   

}

4

2 回答 2

1

我不确定您要完成什么,但请更改:

    public void onClick(View v) {
    String[] recipients = new String[]{"email@email.com", "",};
      String subject = textSubject.getText().toString();
      String message = nametext.getText().toString();
      String message = textMessage.getText().toString();
      String message = textMessage.getText().toString();
      String message = textMessage.getText().toString();

对此:

public void onClick(View v) {
    String[] recipients = new String[]{"email@email.com", "email2@email.com"};
    String subject = textSubject.getText().toString();
    String name = nametext.getText().toString();
    String email = emailtext.getText().toString();
    String phone = phonetext.getText().toString();
    String topics = topicstext.getText().toString();
    String details = deatilstext.getText().toString();

    // Do something with this Strings
}

在这里,每个 EditText 都存储在它自己的 String 中,供您随意使用。

从评论添加

简单地组合字符串,这是一种简单的方法:

String message = nametext.getText().toString();
message += "\n" + emailtext.getText().toString();
etc.

其中 "\n" 是一个换行符,如使用Enter. 您可以在字段之间设置更有趣的格式,也许:

message += "\n\nEmail:\n" + emailtext.getText().toString();

但这取决于你。

于 2012-07-27T21:38:37.020 回答
1

您可以使用 StringBuilder 来撰写电子邮件正文。考虑使用以下代码:

public void onClick(View v) {

    StringBuilder emailBodyBuilder = new StringBuilder();

    emailBodyBuilder.append(textSubject.getText().toString());
    emailBodyBuilder.append("\n");
    emailBodyBuilder.append(nametext.getText().toString());
    emailBodyBuilder.append("\n");
    emailBodyBuilder.append(emailtext.getText().toString());
    emailBodyBuilder.append("\n");
    emailBodyBuilder.append(topicstext.getText().toString());
    emailBodyBuilder.append("\n");
    emailBodyBuilder.append(phonetext.getText().toString());
    emailBodyBuilder.append("\n");
    emailBodyBuilder.append(deatilstext.getText().toString());

    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"email1@domen.com", "email2@domen.com"});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, textSubject.getText().toString());
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailBodyBuilder.toString());
    activity.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}

activity- 是您需要存储才能发送意图的活动参考。如果您onClick在您的Activity.

startActivity(Intent.createChooser(emailIntent, "Send mail..."));

如果您在访问编辑文本视图时开始收到 NullPointer 异常,请确保已初始化它们。就像是:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.email_form_to_support);
    textSubject = (EditText)findViewById(R.id.email);
    ...//and so forth
于 2012-07-27T21:57:28.777 回答