gpt4 book ai didi

自定义 AlertDialog 中 TextView 上的 Android 超链接不可点击

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:00:49 29 4
gpt4 key购买 nike

根据我之前的问题,我仍然在管理自定义 AlertDialog 上的超链接时遇到问题,但我相信我缩小了问题的范围,因为我认为它不起作用只是因为它是一个自定义对话框。

我已遵循所有说明 herehere但似乎无法绕过这种情况

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 &lt;a&gt; 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());

显示如下:

Image 1

这按预期工作并且所有点击都是可点击的。

现在我想要同样的东西,但在自定义布局上。

在我的 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());

上面的代码显示如下(链接不可点击,甚至没有标记为链接):

Image 2

如果我删除

    textView.setMovementMethod(LinkMovementMethod.getInstance());

它显示如下图(链接标记为链接但不可点击):

Image 3

我也尝试过使用 Linkifyandroid:autoLink="web" 但结果总是一样。

setMessage() 可以工作,但使用带有 TextView 的自定义布局无法使其工作。

可能这很简单,但似乎无法实现。有什么建议吗?

谢谢

最佳答案

您定义的 TextView 没有 android:autoLink="all" , android:clickable="true" & android:linksClickable="false"

如下图所示的 info.xml TextView

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/infoDetailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp" />

关于自定义 AlertDialog 中 TextView 上的 Android 超链接不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12069811/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com