Android 如何从字符串资源中获取 AlertDialog 中的可点击超链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1997328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get clickable hyperlinks in AlertDialog from a string resource?
提问by Thilo-Alexander Ginkel
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.
我想要完成的是在AlertDialog
. 虽然AlertDialog
实现愉快地为任何超链接(<a href="...">
在传递给 的字符串资源中定义)加了下划线和颜色,但Builder.setMessage
提供的链接不会变得可点击。
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();
I'd like to avoid using a WebView
to just display a text snippet.
我想避免使用 aWebView
只显示文本片段。
采纳答案by Diego Torres Milano
If you are only showing some text and URL[s] in your dialog perhaps the solution is simpler
如果您只在对话框中显示一些文本和 URL[s],那么解决方案可能更简单
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();
}
}
As shown here http://picasaweb.google.com/lh/photo/up29wTQeK_zuz-LLvre9wQ?feat=directlink
如此处所示 http://picasaweb.google.com/lh/photo/up29wTQeK_zuz-LLvre9wQ?feat=directlink
回答by emmby
I didn't really like the currently most popular answer because it significantly changes the formatting of the message in the dialog.
我不太喜欢当前最流行的答案,因为它显着改变了对话框中消息的格式。
Here's a solution that will linkify your dialog text without otherwise changing the text styling:
这是一个解决方案,它将链接您的对话框文本,而无需更改文本样式:
// Linkify the message
final SpannableString s = new SpannableString(msg); // msg should have url to enable clicking
Linkify.addLinks(s, Linkify.ALL);
final AlertDialog d = new AlertDialog.Builder(activity)
.setPositiveButton(android.R.string.ok, null)
.setIcon(R.drawable.icon)
.setMessage( s )
.create();
d.show();
// Make the textview clickable. Must be called after show()
((TextView)d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
回答by DeRagan
This should make <a href>
tags to get highlighted as well. Please note that i have just added a few lines to emmby's code. so credit to him
这应该使<a href>
标签也突出显示。请注意,我刚刚在 emmby 的代码中添加了几行。所以归功于他
final AlertDialog d = new AlertDialog.Builder(this)
.setPositiveButton(android.R.string.ok, null)
.setIcon(R.drawable.icon)
.setMessage(Html.fromHtml("<a href=\"http://www.google.com\">Check this link out</a>"))
.create();
d.show();
// Make the textview clickable. Must be called after show()
((TextView)d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
回答by vinc3m1
Actually, if you want to simply use a string without dealing with all the views, the fastest way is to find message textview and linkify it:
实际上,如果你想简单地使用一个字符串而不处理所有的视图,最快的方法是找到消息文本视图并链接它:
d.setMessage("Insert your cool string with links and stuff here");
Linkify.addLinks((TextView) d.findViewById(android.R.id.message), Linkify.ALL);
回答by Thilo-Alexander Ginkel
JFTR, here comes the solution which I figured out after some time:
JFTR,这是我一段时间后想出的解决方案:
View view = View.inflate(MainActivity.this, R.layout.about, null);
TextView textView = (TextView) view.findViewById(R.id.message);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(R.string.Text_About);
new AlertDialog.Builder(MainActivity.this).setTitle(
R.string.Title_About).setView(view)
.setPositiveButton(android.R.string.ok, null)
.setIcon(R.drawable.icon).show();
The corresponding about.xml borrowed as a fragment from the Android sources looks like this:
相应的 about.xml 作为从 Android 来源借来的片段如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:paddingTop="2dip"
android:paddingBottom="12dip" android:paddingLeft="14dip"
android:paddingRight="10dip">
<TextView android:id="@+id/message" style="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="5dip" android:linksClickable="true" />
</ScrollView>
The important parts are setting linksClickable to true and setMovementMethod(LinkMovementMethod.getInstance()).
重要的部分是将 linksClickable 设置为 true 和 setMovementMethod(LinkMovementMethod.getInstance())。
回答by neu242
Instead of ...
代替 ...
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle(R.string.my_title);
dialogBuilder.setMessage(R.string.my_text);
... I now use:
...我现在使用:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle(R.string.my_title);
TextView textView = new TextView(this);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(R.string.my_text);
dialogBuilder.setView(textView);
回答by Gabriele
Simplest way:
最简单的方法:
final AlertDialog dlg = new AlertDialog.Builder(this)
.setTitle(R.string.title)
.setMessage(R.string.message)
.setNeutralButton(R.string.close_button, null)
.create();
dlg.show();
// Important! android.R.id.message will be available ONLY AFTER show()
((TextView)dlg.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
回答by Lakshmanan
All the above answer will not remove html tag like , etc if the given string contains, I tried to remove all the tags, and this is work fine for me
如果给定的字符串包含,以上所有答案都不会删除 html 标签,例如,我尝试删除所有标签,这对我来说很好用
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Title");
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setMovementMethod(LinkMovementMethod.getInstance());
text.setText(Html.fromHtml("<b>Hello World</b> This is a test of the URL <a href=http://www.example.com> Example</a><p><b>This text is bold</b></p><p><em>This text is emphasized</em></p><p><code>This is computer output</code></p><p>This is<sub> subscript</sub> and <sup>superscript</sup></p>";));
builder.setView(layout);
AlertDialog alert = builder.show();
and the custom_dialog would be like;
custom_dialog 会像;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
The above code will remove all the html tag and shows Example as Click able URL all others in the specified html formatting text.
上面的代码将删除所有 html 标记,并将示例显示为指定 html 格式文本中的所有其他可点击 URL。
回答by Flow
I was not really satisfied with the current answers. There are two things that are important when you want clickable hyperlinks in href style withing an AlertDialog:
我对目前的答案并不满意。当您想要带有 AlertDialog 的 href 样式的可点击超链接时,有两件事很重要:
- Set the content as View and not with
setMessage(…)
, as only Views allow clickable HTML content - Set the correct movement method (
setMovementMethod(…)
)
- 将内容设置为 View 而不是 with
setMessage(…)
,因为只有 Views 允许可点击的 HTML 内容 - 设置正确的移动方法 (
setMovementMethod(…)
)
Here is a working minimal example:
这是一个有效的最小示例:
strings.xml
字符串.xml
<string name="dialogContent">
Cool Links:\n
<a href="http://stackoverflow.com">Stackoverflow</a>\n
<a href="http://android.stackexchange.com">Android Enthusiasts</a>\n
</string>
MyActivity.java
我的活动.java
…
public void showCoolLinks(View view) {
final TextView textView = new TextView(this);
textView.setText(R.string.dialogContent);
textview.setMovementMethod(LinkMovementMethod.getInstance()); // this is important to make the links clickable
final AlertDialog alertDialog = new AlertDialog.Builder(this)
.setPositivebutton("OK", null)
.setView(textView)
.create();
alertDialog.show()
}
…
回答by Hiroto
I've checked many questions and answers, but it doesn't work. I did it myself. This is the code snippet on MainActivity.java.
我检查了很多问题和答案,但它不起作用。我自己做的。这是 MainActivity.java 上的代码片段。
private void skipToSplashActivity()
{
final TextView textView = new TextView(this);
final SpannableString str = new SpannableString(this.getText(R.string.dialog_message));
textView.setText(str);
textView.setMovementMethod(LinkMovementMethod.getInstance());
....
}
Put this tag on res\values\String.xml
将此标签放在 res\values\String.xml 上
<string name="dialog_message"><a href="http://www.nhk.or.jp/privacy/english/">NHK Policy on Protection of Personal Information</a></string>