我怎样才能从一个字符串资源在AlertDialog点击超链接? [英] How can I get clickable hyperlinks in AlertDialog from a string resource?
问题描述
我试图做到的是要在由AlertDialog显示的信息文本可点击超链接。虽然AlertDialog实施高兴地强调和颜色的任何链接(使用中所定义; A HREF =...>在传递给Builder.setMessage字符串资源),提供的链接不会变成可点击
What I am trying to accomplish is to have clickable hyperlinks in the message text displayed by an AlertDialog. While the AlertDialog implementation happily underlines and colors any hyperlinks (defined using <a href="..."> in the string resource passed to Builder.setMessage) supplied the links do not become clickable.
在code我目前使用看起来像这样:
The code I am currently using looks like this:
new AlertDialog.Builder(MainActivity.this).setTitle(
R.string.Title_About).setMessage(
getResources().getText(R.string.about))
.setPositiveButton(android.R.string.ok, null)
.setIcon(R.drawable.icon).show();
我想避免使用web视图只显示文本片段,所以任何意见如何得到这个工作是非常AP preciated!
I'd like to avoid using a WebView to just display a text snippet, so any advice how to get this working is much appreciated!
推荐答案
如果你只是显示一些文字和网址在对话框[S]也许解决的办法是简单
If you are only showing some text and URL[s] in your dialog perhaps the solution is simpler
public static class MyOtherAlertDialog {
public static AlertDialog create(Context context) {
final TextView message = new TextView(context);
// i.e.: R.string.dialog_message =>
// "Test this dialog following the link to dtmilano.blogspot.com"
final SpannableString s =
new SpannableString(context.getText(R.string.dialog_message));
Linkify.addLinks(s, Linkify.WEB_URLS);
message.setText(s);
message.setMovementMethod(LinkMovementMethod.getInstance());
return new AlertDialog.Builder(context)
.setTitle(R.string.dialog_title)
.setCancelable(true)
.setIcon(android.R.drawable.ic_dialog_info)
.setPositiveButton(R.string.dialog_action_dismiss, null)
.setView(message)
.create();
}
}
如这里所示 <一href="http://picasaweb.google.com/lh/photo/up29wTQeK_zuz-LLvre9wQ?feat=directlink">http://picasaweb.google.com/lh/photo/up29wTQeK_zuz-LLvre9wQ?feat=directlink
As shown here http://picasaweb.google.com/lh/photo/up29wTQeK_zuz-LLvre9wQ?feat=directlink
这篇关于我怎样才能从一个字符串资源在AlertDialog点击超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!