【发布时间】:2012-08-22 09:12:03
【问题描述】:
在我之前的问题之后,我仍然无法在我的自定义 AlertDialog 上管理超链接,但我相信我缩小了问题的范围,因为我认为它只是因为它是一个自定义对话框而不起作用。
我已按照here 和here 的所有说明进行操作,但似乎无法绕过这种情况
在strings.xml 我有一个这样定义的字符串:
<string name="link_text_manual"><b>text2:</b> This is some other
text, with a <a href="http://www.google.com">link</a> specified
via an <a> tag. Use a \"tel:\" URL
to <a href="tel:4155551212">dial a phone number</a>.
</string>
如果我以这种方式创建对话框:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Data")
.setIcon(R.drawable.info)
.setMessage(R.string.link_text_manual)
.setCancelable(true)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog welcomeAlert = builder.create();
welcomeAlert.show();
// Make the textview clickable. Must be called after show()
((TextView)welcomeAlert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
显示如下:
这按预期工作,所有点击都是可点击的。
现在我想要同样的东西,但在自定义布局上。
在我的info.xml 上创建了一个TextView,如下所示:
<TextView
android:id="@+id/infoDetailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/infoVersionTitle"
android:autoLink="all"
android:linksClickable="true"
android:padding="4dp" />
这是代码:
Context ctx = this;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.info, (ViewGroup) findViewById(R.id.infoLayout));
AlertDialog.Builder about = new AlertDialog.Builder(ctx);
about.setView(layout);
about.setTitle("Data");
about.setIcon(R.drawable.info);
AlertDialog displayInfo = about.create();
displayInfo.show();
// Make the textview clickable. Must be called after show()
TextView textView = (TextView) displayInfo.findViewById(R.id.infoDetailText);
textView.setText(R.string.link_text_manual);
textView.setMovementMethod(LinkMovementMethod.getInstance());
上面的代码显示如下(链接不可点击,甚至没有标记为链接):
如果我删除
textView.setMovementMethod(LinkMovementMethod.getInstance());
如下图所示(链接标记为链接但不可点击):
我也尝试过使用Linkify 和android:autoLink="web",但结果始终相同。
setMessage() 可以工作,但带有 TextView 的自定义布局无法正常工作。
可能这很简单,但似乎无法让它发挥作用。有什么建议吗?
谢谢
【问题讨论】:
-
试试 textView.setText(Html.fromHtml(R.string.link_text_manual));并将 android:clickable 属性设置为 xml 中的 textview
-
@hemanthkumar 谢谢。这样做了,但与图 2 完全相同。链接不可点击,甚至未标记为链接
-
@user370305 谢谢,但它总是一样的。根据您的建议,它就像图 2。链接不可点击,甚至未标记为链接
-
@user370305 没有
textView.setMovementMethod(LinkMovementMethod.getInstance());,textView.setText(Html.fromHtml("Click <a href=\"http://www.poon-world.com\">here</a> to switch on the red light.\n"));就像图片3。链接标记为链接但不可点击。使用textView.setMovementMethod(LinkMovementMethod.getInstance());,如图 2 所示。
标签: android android-alertdialog