TextView to send email when clicked
I have a TextView with android:autoLink="e开发者_如何学编程mail".
If I put my email address in there then a link appears that I can click.
How do I have different text appear (for example 'Send Feedback') instead of the email address but still behave the same when clicked?
Thanks
To achieve what I wanted required a different approach:
TextView feedback = (TextView) findViewById(R.id.TextViewSendFeedback);
feedback.setText(Html.fromHtml("<a href=\"mailto:ask@me.it\">Send Feedback</a>"));
feedback.setMovementMethod(LinkMovementMethod.getInstance());
This basically places HTML in the TextView so I get a link saying 'Send Feedback' but clicking it opens the default email application.
Word of warning: Trying this in the emulator didn't initially work for me, saying it was unsupported. This was just because I didn't have an email account setup. Setting one up in the emulator made the link work as I wanted.
You can use both links and email if you set the following param in the TextView
android:autoLink="web|email"
the links will be opened in the browser and the mails will be sent by the default mail client
Another simple way in layout:
...
<TextView
android:id="@+id/tvTelefone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sobre_telefone"
android:textColor="#000000"
android:autoLink="phone" />
...
...
<string name="sobre_telefone">Contato: (45) 9145-0000</string>
}
Read more here: http://developer.android.com/reference/android/widget/TextView.html#attr_android:autoLink
It might be easier to create a button and inside your onClickListener() pull an email from maybe R.string.email.
Fro the Strings From strings.xml
:
<string name="your_string"><![CDATA[ contact us at <a href=\"mailto:recipient@mail.com\">recipient@mail.com</a> for more help.]]></string>
tvObject.setText(Html.fromHtml(getString(R.string.your_string)));
tvObject.setMovementMethod(LinkMovementMethod.getInstance());
精彩评论