我想要完成的是在一个显示的消息文本中有可点击的超链接AlertDialog
.虽然AlertDialog
实现愉快地强调并着色任何超链接(在传递给的字符串资源中定义
Builder.setMessage
),但链接不会变得可点击.
我目前使用的代码如下所示:
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
只显示文本片段.
我不太喜欢当前最流行的答案,因为它会显着改变对话框中消息的格式.
这是一个解决方案,它将链接您的对话框文本,而不会改变文本样式:
// 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());
如果您只是在对话框中显示一些文本和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(); } }
如此处所示 http://picasaweb.google.com/lh/photo/up29wTQeK_zuz-LLvre9wQ?feat=directlink
这应该使标签也被突出显示.请注意,我刚刚为emmby的代码添加了几行代码.所以归功于他
final AlertDialog d = new AlertDialog.Builder(this) .setPositiveButton(android.R.string.ok, null) .setIcon(R.drawable.icon) .setMessage(Html.fromHtml("Check this link out")) .create(); d.show(); // Make the textview clickable. Must be called after show() ((TextView)d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
实际上,如果您只想在不处理所有视图的情况下使用字符串,最快的方法是查找消息textview并将其链接:
d.setMessage("Insert your cool string with links and stuff here"); Linkify.addLinks((TextView) d.findViewById(android.R.id.message), Linkify.ALL);
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();
从Android源代码片段借用的相应about.xml如下所示:
重要的部分是将linksClickable设置为true和setMovementMethod(LinkMovementMethod.getInstance()).
代替 ...
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); dialogBuilder.setTitle(R.string.my_title); dialogBuilder.setMessage(R.string.my_text);
......我现在用:
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);
以上所有答案都不会删除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("Hello World This is a test of the URL ExampleThis text is bold
This text is emphasized
This is computer output
This is subscript and superscript
";)); builder.setView(layout); AlertDialog alert = builder.show();
和custom_dialog会是这样的;
上面的代码将删除所有的html标记,并在指定的html格式文本中显示Example as Click able URL.
我对目前的答案并不满意.当你想要一个带有AlertDialog的href样式的可点击超链接时,有两件事是很重要的:
将内容设置为View而不是setMessage(…)
,因为只有Views允许可点击的HTML内容
设置正确的移动方法(setMovementMethod(…)
)
这是一个有用的最小例子:
strings.xml中
Cool Links:\n Stackoverflow\n Android Enthusiasts\n
MyActivity.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() } …
最简单的方法:
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());