[英]How to make ClickableSpan links in MaterialAlertDialogBuilder clickable?
I have a SpannableStringBuilder
object with a ClickableSpan
link like this,我有一个SpannableStringBuilder
object 和这样的ClickableSpan
链接,
SpannableStringBuilder ssb = new SpannableStringBuilder("Hi StackOverflow!");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(@NonNull View widget) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
};
ssb.setSpan(clickableSpan, 3, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
This works fine when I set it to a TextView
and after using textView.setMovementMethod(LinkMovementMethod.getInstance())
当我将其设置为TextView
并在使用textView.setMovementMethod(LinkMovementMethod.getInstance())
之后,这工作正常
When I set the ssb
object to the MaterialAlertDialogBuilder
,当我将ssb
object 设置为MaterialAlertDialogBuilder
ilder 时,
new MaterialAlertDialogBuilder(this)
.setTitle("My Dialog")
.setMessage(ssb)
.show();
It displays as a clickable link but cannot actually click it它显示为可点击的链接,但实际上无法点击它 I couldn't find a way to use setMovementMethod
for the message in MaterialAlertDialogBuilder
.我找不到对MaterialAlertDialogBuilder
ilder 中的消息使用setMovementMethod
的方法。 Is there any way to make it clickable?有什么办法让它可以点击吗?
Here is code snippet to make your spannable content clickable, give it a try.这是使您的可扩展内容可点击的代码片段,试一试。
SpannableString s = new SpannableString(msg); // Here msg should have url to enable clicking
Linkify.addLinks(s, Linkify.ALL);
After that put your alertdialog code here之后将您的警报对话框代码放在这里
//Alert dialog code
Then get id of textview of your MaterialAlertDialog, below line of code must be called after dialog.show() like this,然后获取您的 MaterialAlertDialog 的 textview 的 id,下面的代码行必须在 dialog.show() 之后像这样调用,
((TextView)dialog.findViewById(android.R.id.message))
.setMovementMethod(LinkMovementMethod.getInstance());
Just define a TextView
with the default style ( @style/MaterialAlertDialog.MaterialComponents.Body.Text
) provided by the MaterialAlertDialogBuilder
:只需使用 MaterialAlertDialogBuilder 提供的默认样式( @style/MaterialAlertDialog.MaterialComponents.Body.Text
MaterialAlertDialogBuilder
定义一个TextView
:
<TextView
android:id="@+id/textview1"
style="?attr/materialAlertDialogBodyTextStyle"
...>
Then set the SpannableStringBuilder
as text:然后将SpannableStringBuilder
设置为文本:
SpannableStringBuilder ssb =....
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom_text_view, null);
TextView customTextView = dialogView.findViewById(R.id.textview1);
customTextView.setText(ssb);
customTextView.setMovementMethod(LinkMovementMethod.getInstance());
Finally:最后:
MaterialAlertDialogBuilder(this)
.setTitle("My Dialog")
.setView(dialogView)
...
I was wondering how to add a hyperlink to a MaterialAlertDialog
message via a static string that contains simple html tags without having to create a custom view (eg Hello <a href='https://en.wikipedia.org/wiki/%22Hello,_World!%22_program'> World </a>
), @Bhavnik's answer helped a lot:我想知道如何通过包含简单html 标签的 static 字符串向MaterialAlertDialog
消息添加超链接,而无需创建自定义视图(例如Hello <a href='https://en.wikipedia.org/wiki/%22Hello,_World!%22_program'> World </a>
), @Bhavnik 的回答很有帮助:
val dialog = MaterialAlertDialogBuilder(context)
.setTitle(getString(R.string.dialog_title))
.setMessage(getText(R.string.dialog_content))
...
.show()
dialog.findViewById<TextView>(android.R.id.message)?.movementMethod = LinkMovementMethod.getInstance()
The main trick is indeed to add the LinkMovementMethod
after showing the dialog, but also don't forget to get the string with getText
in order to keep the styling.主要技巧确实是在显示对话框后添加LinkMovementMethod
,但也不要忘记使用getText
获取字符串以保持样式。
For more complex html, .setMessage(Html.fromHtml(getText(R.string.dialog_content).toString(), flags))
also seems to work.对于更复杂的 html, .setMessage(Html.fromHtml(getText(R.string.dialog_content).toString(), flags))
似乎也有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.