繁体   English   中英

将文本从编辑文本字段添加到电子邮件中

[英]Adding text from edit text field into an email

我有一个应用,希望与我们联系,以供用户输入其姓名,地址,电话号码和评论部分。 然后,他们将单击“撰写邮件”按钮,它将自动将文本加载到电子邮件中。 制定了一些代码,但不确定如何将文本从编辑文本输入到我的电子邮件消息中。 任何人都对我如何做到这一点有任何建议。 我已经附上了我的xml文件和java文件中的代码。

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;

 public class Contactform extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contactform);

    Button email = (Button) findViewById(R.id.button30);
    email.setOnClickListener(new View.OnClickListener()  {

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

            /* Fill it with Data */
            email.setType("plain/text");
            email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"ladsoffice@lads-to-leaders.org"});
            email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Lads to Leaders/Leaderettes Questions and/or Comments");
            email.putExtra(android.content.Intent.EXTRA_TEXT, "Sent from the Lads to Leaders/Leaderettes Android App");

            /* Send it off to the Activity-Chooser */
            startActivity(Intent.createChooser(email, "Send mail..."));
        }
    });
}


 }

这是xml文件

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#bf311a" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="18dp"
        android:text="@string/name"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="37dp"
        android:layout_toRightOf="@+id/textView1"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="35dp"
        android:text="@string/addy"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignParentRight="true"
        android:inputType="textPostalAddress" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="30dp"
        android:textColor="#000000"
        android:text="@string/cell"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/editText2"
        android:inputType="phone" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:textColor="#000000"
        android:text="@string/questions"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="25dp"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/button30"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="47dp"
        android:background="@drawable/composemail" />

</RelativeLayout>

这是我的表格看起来的一个ss,如果有帮助

在此处输入图片说明

感谢您提供的任何帮助。 仍在学习如何做一些这样的事情,所以我很抱歉,如果以前有人问过这个问题,但是已经搜索了两天,似乎找不到我需要做的事情。

好吧,对于那些想知道这里是正确代码的人。 我需要修改的唯一代码是Java文件的代码,因此,我将发布的唯一代码。

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;

 public class Contactform extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contactform);

    final EditText name = (EditText) findViewById(R.id.editText1);
    final EditText addy = (EditText) findViewById(R.id.editText2);
    final EditText cell = (EditText) findViewById(R.id.editText3);
    final EditText questions = (EditText) findViewById(R.id.editText4);

    Button email = (Button) findViewById(R.id.button30);
    email.setOnClickListener(new View.OnClickListener()  {

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

            /* Fill it with Data */
            email.setType("plain/text");
            email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"ladsoffice@lads-to-leaders.org"});
            email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Lads to Leaders/Leaderettes Questions and/or Comments");
            email.putExtra(android.content.Intent.EXTRA_TEXT, 
                    "name:"+name.getText().toString()+'\n'+"address:"+addy.getText().toString()+'\n'+"phone:"+cell.getText().toString()+'\n'+'\n'+questions.getText().toString()+'\n'+'\n'+"Sent from the Lads to Leaders/Leaderettes Android App.");

            /* Send it off to the Activity-Chooser */
            startActivity(Intent.createChooser(email, "Send mail..."));
        }
    });
}


 }

好的,所以快速解释一下。 您必须在oncreate方法之后在代码中声明您的编辑文本。 它也必须在其前面说“ final”。 然后,将字符串输入EXTRA TEXT的意图中。 我也用单引号添加了“ \\ n”。 此操作将充当输入按钮,为每个项目提供单独的行,例如,我只希望在一行上命名,因此我确实要输入name + mystring +'\\ n'。 这将使您的名称和字符串显示在一行上,然后在一行下移到下一行。 希望这会有所帮助,并能帮助其他人。

您可以将所有数据放入email.putExtra(android.content.Intent.EXTRA_TEXT, "Sent from the Lads to Leaders/Leaderettes Android App");

首先在oncreate的ID中通过ID获取所有EditText

 EditText name = (EditText) findViewById(R.id.editext1);
 EditText address = (EditText) findViewById(R.id.editext2);
 EditText phone = (EditText) findViewById(R.id.editext3);

然后把所有数据

email.putExtra(android.content.Intent.EXTRA_TEXT, 
"name:"+name.getText().toString()+"address:"+address.getText().toString()+"phone:
 "+phone.getText().toString());

请享用 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM