我想要完成的是在AlertDialog
显示的消息文本中有可点击的超链接。虽然AlertDialog
实现很乐意为任何超链接(在传递给<a href="...">
的字符串资源中使用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
来显示文字摘要。
答案 0 :(得分:195)
我真的不喜欢当前最受欢迎的答案,因为它会显着改变对话框中消息的格式。
这是一个解决方案,它将链接您的对话框文本,而不会改变文本样式:
// Linkify the message
final SpannableString s = new SpannableString(msg);
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());
答案 1 :(得分:122)
如果您只是在对话框中显示一些文字和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
答案 2 :(得分:42)
这也应该使<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());
答案 3 :(得分:13)
实际上,如果您只想在不处理所有视图的情况下使用字符串,最快的方法是找到消息textview并将其链接起来:
d.setMessage("Insert your cool string with links and stuff here");
Linkify.addLinks((TextView) d.findViewById(android.R.id.message), Linkify.ALL);
答案 4 :(得分:12)
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如下所示:
<?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>
重要的部分是将linksClickable设置为true和setMovementMethod(LinkMovementMethod.getInstance())。
答案 5 :(得分:9)
而不是......
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);
答案 6 :(得分:6)
以上所有答案都不会删除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();
,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>
以上代码将删除所有html标记,并在指定的html格式文本中显示示例为Clickable URL所有其他内容。
答案 7 :(得分:5)
我对目前的答案并不满意。当你想要一个带有AlertDialog的href样式的可点击超链接时,有两件事是重要的:
setMessage(…)
,因为只有视图允许可点击的HTML内容setMovementMethod(…)
)这是一个有用的最小例子:
<强>的strings.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 强>
…
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()
}
…
答案 8 :(得分:4)
最简单的方法:
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());
答案 9 :(得分:3)
&#39; ave检查了很多问题和答案,但它不起作用。我自己做的。这是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());
....
}
将此标记放在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>
答案 10 :(得分:2)
我结合了上面讨论的一些选项来提出适用于我的这个功能。将结果传递给对话框构建器的SetView()方法。
public ScrollView LinkifyText(String message)
{
ScrollView svMessage = new ScrollView(this);
TextView tvMessage = new TextView(this);
SpannableString spanText = new SpannableString(message);
Linkify.addLinks(spanText, Linkify.ALL);
tvMessage.setText(spanText);
tvMessage.setMovementMethod(LinkMovementMethod.getInstance());
svMessage.setPadding(14, 2, 10, 12);
svMessage.addView(tvMessage);
return svMessage;
}
答案 11 :(得分:2)
如果您使用DialogFragment
,此解决方案应该有所帮助。
public class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// dialog_text contains "This is a http://test.org/"
String msg = getResources().getString(R.string.dialog_text);
SpannableString spanMsg = new SpannableString(msg);
Linkify.addLinks(spanMsg, Linkify.ALL);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog_title)
.setMessage(spanMsg)
.setPositiveButton(R.string.ok, null);
return builder.create();
}
@Override
public void onStart() {
super.onStart();
// Make the dialog's TextView clickable
((TextView)this.getDialog().findViewById(android.R.id.message))
.setMovementMethod(LinkMovementMethod.getInstance());
}
}
答案 12 :(得分:1)
我通过在XML资源中指定警报框并加载它来完成此操作。例如,请参阅在about.xml末尾附近实例化的ChandlerQE.java(请参阅ABOUT_URL ID)。 java代码中的相关部分:
LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(R.layout.about, null);
new AlertDialog.Builder(ChandlerQE.this)
.setTitle(R.string.about)
.setView(view)
答案 13 :(得分:1)
对我来说,创建隐私策略对话框的最佳解决方案是:
private void showPrivacyDialog() {
if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(PRIVACY_DIALOG_SHOWN, false)) {
String privacy_pol = "<a href='https://sites.google.com/view/aiqprivacypolicy/home'> Privacy Policy </a>";
String toc = "<a href='https://sites.google.com/view/aiqprivacypolicy/home'> T&C </a>";
AlertDialog dialog = new AlertDialog.Builder(this)
.setMessage(Html.fromHtml("By using this application, you agree to " + privacy_pol + " and " + toc + " of this application."))
.setPositiveButton("ACCEPT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean(PRIVACY_DIALOG_SHOWN, true).apply();
}
})
.setNegativeButton("DECLINE", null)
.setCancelable(false)
.create();
dialog.show();
TextView textView = dialog.findViewById(android.R.id.message);
textView.setLinksClickable(true);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
检查工作示例:app link
答案 14 :(得分:0)
这是我的解决方案。它创建了一个普通链接,其中没有html标记,也没有任何可见的URL。它还可以使设计保持完整。
SpannableString s = new SpannableString("This is my link.");
s.setSpan(new URLSpan("http://www.google.com"), 11, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(this);
}
final AlertDialog d = builder
.setPositiveButton("CLOSE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing, just close
}
})
.setNegativeButton("SHARE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Share the app
share("Subject", "Text");
}
})
.setIcon(R.drawable.photo_profile)
.setMessage(s)
.setTitle(R.string.about_title)
.create();
d.show();
((TextView)d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
答案 15 :(得分:0)
最简单,最短的方法就是这样
editsubmit(){
this.data.client_id = 1;
this.notify.clear();
this.notify.info("Wait...", {timeout: 0});
this.api.put('users/'+this.data.id, this.data, this.headers).subscribe(
data => {
this.notify.clear();
this.notify.info("User Updated Successfully", {timeout: 2000});
this.ngOnInit();
this.closeEditModal();
},
error => { this.notify.clear(); this.error = error.error.errors; }
);
}
答案 16 :(得分:0)
这是我使用的简单方法
strings.xml中的字符串
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="credits_title">Credits</string>
<string name="confirm">OK</string>
<string name="credits">All rights reserved.
<a href="https://google.com">Source</a>
</string>
</resources>
dimens.xml中的维度
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<dimen name="margin_8dp">8dp</dimen>
<dimen name="margin_32dp">32dp</dimen>
</resources>
助手对话框类
public class MessageHelper {
public static void showCreditsDialog(Context context) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(R.string.credits_title);
TextView textView = new TextView(context);
int padding = (int) context.getResources().getDimension(R.dimen.margin_32dp);
int topPadding = (int) context.getResources().getDimension(R.dimen.margin_8dp);
textView.setPadding(padding, topPadding, padding, 0);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(R.string.credits);
alertDialog.setView(textView);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, context.getResources().getString(R.string.confirm),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}
使用方法
MessageHelper.showCreditsDialog(this); // this is the context
预览
答案 17 :(得分:0)
字符串资源:
<string name="foo"><a href="https://www.google.com/">some link</a></string>
代码:
AlertDialog.Builder(context)
.setMessage(R.string.foo)
.show()
.apply {
findViewById<TextView>(android.R.id.message)
?.movementMethod = LinkMovementMethod.getInstance()
}