Android按钮onclick提交到电子邮件

时间:2012-01-24 21:31:44

标签: android form-submit email-client

我已经使用Java和Xml几个月了,感谢StackOverflow上的每个人都提供了很多帮助。

我的问题是关于提交按钮的android的java编程。

目前我正在尝试弄清楚如何将值提交到电子邮件地址(幕后)

假设我们有一个文本字段和一个按钮;我想获取在文本字段中输入的值,并将其提交到电子邮件地址onclick。

我无法在网上找到任何显示如何执行此操作的内容。

提前感谢您阅读我的帖子,我期待您的建议。

4 个答案:

答案 0 :(得分:10)

这是一个很好的例子,说明如何使用Intents非常方便! Android有一堆预先定义的Intent,可以在系统中执行某些操作;您可能之前点击了一张图片,并弹出一个对话框,询问您是要在图库中还是在Astro等第三方应用中查看。观看图像有其预定的意图。

发送电子邮件也有其预先确定的意图:android.content.Intent.ACTION_SEND。您需要使用该属性创建一个intent,然后附加额外的信息(即要发送到的地址,主题/消息正文等)。

示例代码:

// Data members
private Intent emailIntent;
private String feedback;
private EditText feedbackBox;

// Create the Intent, and give it the pre-defined value
// that the Android machine automatically associates with
// sending an email.
emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");

// Put extra information into the Intent, including the email address
// that you wish to send to, and any subject (optional, of course).
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"your_email@whatever.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Insert subject here");

// Acquire feedback from an EditText and save it to a String.
feedback = feedbackBox.getText().toString();

// Put the message into the Intent as more extra information,                   
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, feedback);

// Start the Intent, which will launch the user's email 
// app (make sure you save any necessary information in YOUR app
// in your onPause() method, as launching the email Intent will
// pause your app). This will create what I discussed above - a
// popup box that the user can use to determine which app they would like
// to use in order to send the email.
startActivity(Intent.createChooser(emailIntent, "Insert title for dialog box."));

我希望这有帮助!!

您可能想查看一些来源:
http://developer.android.com/guide/topics/intents/intents-filters.html
http://developer.android.com/reference/android/content/Intent.html#ACTION_SEND

答案 1 :(得分:1)

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, someEditText.getText());
startActivity(Intent.createChooser(emailIntent, "Send someone an email..."));

答案 2 :(得分:0)

试试这个

Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:Type email address here"));
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }

答案 3 :(得分:0)

效果很好

 String   mail=mailid.getText().toString();
 String   msubject=subject.getText().toString();
 String   mbody=body.getText().toString();
    Log.i("Send email", "");
    String[] TO = {mail};
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Static subject "+ msubject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, "Static body "+ mbody);

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

    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MainActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
    }