开发者

How to send email by clicking text view links

I have to show a normal text as a link and if I click the link it should open the mail page with "TO" mail address auto filled. I showed the text view as a link as follows:

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

But it isn't working for me. The words came as a link but are not showing mail intent while clic开发者_开发知识库king the link.


If you set the "autoLink" property in the TextView in your layout it works and looks much easier:

android:autoLink="email"


You can open an email intent and bind onClick event on textView.

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


I have use the fallowing code for sending email you should change according to your condition.

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();
                    }
                }
            }
        }
    });
}

}

I hope it is help full to you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜