如何从字符串资源中获取 AlertDialog 中的可点击超链接? [英] How can I get clickable hyperlinks in AlertDialog from a string resource?
问题描述
我想要完成的是在 AlertDialog
显示的消息文本中包含可点击的超链接.虽然 AlertDialog
实现愉快地为任何超链接(在传递给 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.
我目前使用的代码如下所示:
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();
我想避免使用 WebView
来仅显示文本片段.
I'd like to avoid using a WebView
to just display a text snippet.
推荐答案
如果您只在对话框中显示一些文本和 URL[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();
}
}
如图所示http://picasaweb.google.com/lh/photo/up29wTQeK_zuz-LLvre9wQ?feat=directlink
这篇关于如何从字符串资源中获取 AlertDialog 中的可点击超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!