android - 如何使 MaterialAlertDialogBuilder 中的 ClickableSpan 链接可点击?
问题描述
我有一个SpannableStringBuilder
带有这样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);
当我将其设置为 aTextView
并使用后,这工作正常textView.setMovementMethod(LinkMovementMethod.getInstance())
当我将ssb
对象设置MaterialAlertDialogBuilder
为
new MaterialAlertDialogBuilder(this)
.setTitle("My Dialog")
.setMessage(ssb)
.show();
它显示为可单击的链接,但实际上无法单击它
我找不到setMovementMethod
用于MaterialAlertDialogBuilder
. 有什么办法让它可以点击吗?
解决方案
这是使您的可扩展内容可点击的代码片段,试一试。
SpannableString s = new SpannableString(msg); // Here msg should have url to enable clicking
Linkify.addLinks(s, Linkify.ALL);
之后将您的警报对话框代码放在这里
//Alert dialog code
然后获取 MaterialAlertDialog 的 textview 的 id,下面的代码行必须在 dialog.show() 之后像这样调用,
((TextView)dialog.findViewById(android.R.id.message))
.setMovementMethod(LinkMovementMethod.getInstance());
推荐阅读
- django - JWT Token过期时间增加
- javascript - Angular 中的 [class]="'success'" 和 class="success" 有区别吗?
- android - 如何设置 rtl=false ,仅用于图层?
- r - 将列表对象列表转换为R中的数据框
- haskell - 数据类型的别名
- css - 使用宽度计算Angular 6动态设置CSS属性左侧
- raku - 使用 NativeCall 将 CStruct 中的内联 CArray 传递给共享库
- facebook - Google Chrome / Safari 打开时,相同的 URL 显示两个不同的图像?
- jquery - 使用 jQuery Touch Events (Ben Major) 捕获滑动事件
- c# - 如何在 ASP.NET Core MVC 中获取会话超时通知?