我正在尝试通过电子邮件发送多个 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..."));
}
});
}
}