开发者

Capture user input and send them directly to an email address using button

I am currently working on an Android application which has an order form. I would like to know how I can allow the application to capture what the user has input and send the input directly to an email using button, without bringing the user to the email app page. I know my .java class has errors, however, I am trying out... Below is my xml (layout) file and my .java class file. Please help me... Thanks!!

Here is my .xml file (layout)

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:stretchColumns="1">
<TextView
    android:id="@+id/txtname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name: "
    />
<EditText
    android:id="@+id/editname"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtname"
    android:hint="Your name please"
    />
<TextView
    android:id="@+id/txtemail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/editname"
    android:text="Email Address: "
开发者_如何学编程    />
<EditText
    android:id="@+id/editemail"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtemail"
    android:hint="Enter email address here"
    />
<TextView
    android:id="@+id/txtnum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/editemail"
    android:text="Contact Number: "
    />
<EditText
    android:id="@+id/editnum"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtnum"
    android:hint="Enter contact number here"
    />
<TextView
    android:id="@+id/txtadd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/editnum"
    android:text="Mailing Address: "
    />
<EditText
    android:id="@+id/editadd"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtadd"
    android:hint="Enter mailing address here"
    />
<TextView
    android:id="@+id/txtitems"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/editadd"
    android:text="Item(s): "
    />
<EditText
    android:id="@+id/edititems"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtadd"
    android:hint="Enter item(s) here"
    />
<Button
    android:id="@+id/btn_submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/edititems"
    android:text="Submit form"
    />
</TableLayout>

Here is my .java class codes

public class OrderFormActivity extends Activity implements OnClickListener{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.orderform);

    Button btnsubmit = (Button)findViewById(R.id.btn_submit);
    btnsubmit.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == this.findViewById(R.id.btn_submit)) {

    EditText name = (EditText)findViewById(R.id.editname);
    String strname = name.getText().toString();

    EditText email = (EditText)findViewById(R.id.editemail);
    String stremail = email.getText().toString();

    EditText num = (EditText)findViewById(R.id.editnum);
    String strnum = num.getText().toString();

    EditText add = (EditText)findViewById(R.id.editadd);
    String stradd = add.getText().toString();

    EditText items = (EditText)findViewById(R.id.edititems);
    String stritems = items.getText().toString();

    showOrder(strname, stremail, strnum, stradd,stritems);
    }

}

private void showOrder (String strname, String stremail, String strnum, String stradd, String stritems) {
    String result = strname + stremail + strnum + stradd + stritems;

    //Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

    String emailAddress = "kwok.winona@gmail.com";

    Intent intent = new Intent(Intent.ACTION_SEND);  
    intent.setType("plain/text");
    intent.putExtra(Intent.EXTRA_EMAIL, emailAddress);    
    intent.putExtra(Intent.EXTRA_TEXT, result);         

}

}

Thanks all again in advance!!! :D


There is no API to send an email through intents without user interaction. This has been repeated several times here in SO.

As pearsonartphoto suggested, you need to use an external library to send the email. JavaMail is the clear first option, even if you "found that solution too cumbersome".

So you need to gather all the fields as you posted, format the in a useful way for the email (perhaps xml, perhaps JSON?) and send it as text with JavaMail.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜