将文本从编辑文本字段添加到电子邮件中 [英] Adding text from edit text field into an email

查看:331
本文介绍了将文本从编辑文本字段添加到电子邮件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,希望与用户输入他们的姓名,地址,电话号码和评论部分联系。然后他们将点击撰写邮件按钮,它将自动将文本加载到电子邮件中。有一些代码解决了,但不知道如何将文本从编辑文本到我的电子邮件msg。任何人都有任何建议,我如何能做到这一点。我附加了我的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自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.contactform);

按钮电子邮件=(Button)findViewById(R.id.button30);
email.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v){
// TODO自动生成的方法存根
Intent email = new Intent(android.content.Intent.ACTION_SEND);

/ *填充数据* /
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, 给领导者/领导者问题和/或评论);
email.putExtra(android.content.Intent.EXTRA_TEXT,从小伙子发送到领导/领导者Android应用);

/ *发送到活动选择器* /
startActivity(Intent.createChooser(email,Send mail ...));
}
});
}


}

这里是xml文件

 <?xml version =1.0encoding =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_centerHorizo​​ntal =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_centerHorizo​​ntal =true
android:layout_marginTop =47dp
android:background =@ drawable / composemail >

< / RelativeLayout>

如果这有助于

$ b $,这里是我的表单看起来像b



感谢您给予任何帮助。仍然在学习一些如何做一些这样的东西,所以我道歉,如果这已经被问过,但搜索了两天,似乎找不到我需要做的这一点。



对于那些想知道这里的人来说,是正确的代码。我需要修改的唯一代码是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自动生成的方法存根
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)findViewById(R.id.button30);
email.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v){
// TODO自动生成的方法存根
Intent email = new Intent(android.content.Intent.ACTION_SEND);

/ *填充数据* /
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, to Leaders / Leaderettes Questions and / or Comments);
email.putExtra(android.content.Intent.EXTRA_TEXT,
name:+ name.getText()。toString()+' \\\
'+ 地址: + addy.getText()的toString()+' \\\
'+ 电话: + cell.getText()的toString()+' \\\
'+' \ n'+ questions.getText()。toString()+'\\\
'+'\\\
'+从小伙子发送到领导/ Leaderettes Android应用程序);

/ *发送到活动选择器* /
startActivity(Intent.createChooser(email,Send mail ...));
}
});
}


}

一个快速的解释。您必须在您的oncreate方法之后在代码中声明您的编辑文本。它也必须在前面说最后。然后你输入你的字符串到EXTRA TEXT的意图。我也添加了'\'在单引号。此操作将作为一个输入按钮,给每个项目自己的行,例如我只想在一行上命名,所以我做了名字+ mystring +'\\\
'。这将使您的姓名和字符串出现在一行,然后下一行下一行。希望这样做可以帮助其他人。

解决方案

您可以将所有数据放在电子邮件中。 putExtra(android.content.Intent.EXTRA_TEXT,从小伙子发送到领导/ Leaderettes Android应用程序);



首先获取oncreate中的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());

享受:)


I have an app and want to have a contact us for where the user inputs their name, address, phone number, and a comment section. Then they will click on compose mail button and it will load the text into the email automaticlly. have some of the code worked out but not sure how to get the text from the edit text into my email msg. anyone have any suggestions on how i can do this. I have attached the code from my xml file and my java file.

 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..."));
        }
    });
}


 }

and here is the xml file

  <?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>

and here is a ss of what my form looks like if that helps

Thank you for any help you can give. Still learning a bit on how to do some of this stuff so i apologize if this has been asked before but been searching for two days and can't seem to find what i need to do this.

Ok for those who want to know here is the corrected code. The only code i needed to modify was the code for the Java file so thats the only code i will post.

 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..."));
        }
    });
}


 }

Ok so just a quick explaintion. you have to declare your edit text in the code after your oncreate method. It must say final in front of it as well. Then you input your strings into the intent for EXTRA TEXT. also i added the '\n' in single quotes. this action will act as an enter button giving each item its own line so for example i only wanted name on one line so i did name+mystring+'\n'. this will make your name and string appear on one line and then go down one line for the next line. Hope this makes since and it helps someone else out.

解决方案

You can put all data inside email.putExtra(android.content.Intent.EXTRA_TEXT, "Sent from the Lads to Leaders/Leaderettes Android App");

First Get all EditText by ID here in oncreate Like

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

Then put all data

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

Enjoy :)

这篇关于将文本从编辑文本字段添加到电子邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆