如何通过单击文本视图链接发送电子邮件

时间:2011-06-23 06:23:40

标签: android

我有一个显示正常文本作为链接,如果我点击链接它应该打开邮件页面“TO”邮件地址应自动填写。我将文本视图显示为如下链接

      TextView EmailLink;    
      EmailLink = (TextView) findViewById(R.id.lblPrivacyPara21);
      EmailLink.setText(Html.fromHtml("hello <a href=\"mailto:vigneshdharma@gmail.com\">my@email.com</a>"));

但它对我不起作用。这些单词作为链接出现,但在单击链接时未显示邮件意图。请帮助我。

3 个答案:

答案 0 :(得分:6)

如果在布局中的TextView中设置“autoLink”属性,它可以工作并且看起来更容易:

机器人:自动链接= “电子邮件”

答案 1 :(得分:0)

您可以打开电子邮件意图并在textView上绑定onClick事件。

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
startActivity(emailIntent); 

答案 2 :(得分:-1)

我使用以下代码发送电子邮件,您应该根据自己的情况进行更改。

public class ContactUSActivity extends MenuActivity {
private HttpURLConnection conn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contactus);

    final EditText nameText = (EditText) findViewById(R.id.contactnametextContectUS);
    final EditText emailText = (EditText) findViewById(R.id.emailaddresstextContectUS);
    final EditText commentText = (EditText) findViewById(R.id.commenttextContectUS);

    Button submitBtn = (Button) findViewById(R.id.ButtonLoginContectUS);

    submitBtn.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            if (GUIStatiMethods.connectionCheck(ContactUSActivity.this)) {
                String name = nameText.getText().toString();
                String email = emailText.getText().toString();
                String comment = commentText.getText().toString();
                Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
                Matcher matcher = pattern.matcher(email);
                boolean matchFound = matcher.matches();
                if (name.equalsIgnoreCase("")) {
                    GUIStatiMethods.showMessageDialog(
                            ContactUSActivity.this, "Please enter name");
                } else if (name.length() > 20) {
                    GUIStatiMethods.showMessageDialog(
                            ContactUSActivity.this,
                            "Name should be less then 20 character");
                } else if (email.equalsIgnoreCase("")) {
                    GUIStatiMethods.showMessageDialog(
                            ContactUSActivity.this, "Please enter email");
                } else if (!(matchFound)) {
                    GUIStatiMethods.showMessageDialog(
                            ContactUSActivity.this,
                            "Invalid email address.");
                } else if (comment.equalsIgnoreCase("")) {
                    GUIStatiMethods.showMessageDialog(
                            ContactUSActivity.this, "Please enter comment");
                } else {
                    try {
                        URL url = new URL(UrlStatics.BASEURL_MAIN_SERVER
                                + "IGA_ADD_CONTACTUS&contactName=" + name
                                + "&email=" + email + "&comments="
                                + comment);
                        conn = (HttpURLConnection) url.openConnection();
                        conn.setDoInput(true);
                        conn.setDoOutput(true);
                        conn.setRequestMethod("POST");
                        conn.setRequestProperty("Connection", "Keep-Alive");
                        OutputStreamWriter out = new OutputStreamWriter(
                                conn.getOutputStream());
                        out.write("Content-Disposition: post-data;&contactName="
                                + name
                                + "&email="
                                + email
                                + "&comments="
                                + comment);
                        out.close();
                        BufferedReader rd = new BufferedReader(
                                new InputStreamReader(conn.getInputStream()));
                        String decodedString;
                        while ((decodedString = rd.readLine()) != null) {
                            Log.v("TAG", "Contact is Added" + decodedString);
                        }
                        AlertDialog.Builder dialog = new Builder(
                                ContactUSActivity.this);
                        dialog.setTitle("Thank You!!");
                        dialog.setMessage("We will contact you shortly");
                        dialog.setCancelable(false);
                        dialog.setPositiveButton("Ok",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        nameText.setText("");
                                        emailText.setText("");
                                        commentText.setText("");
                                        finish();
                                    }
                                });
                        dialog.show();
                        rd.close();

                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (ProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    });
}

}

我希望对你有所帮助。