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

标签 android android-alertdialog

根据我之前的问题,我仍然在管理自定义 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/

相关文章:

android - 仅在选中项目时激活 AlertDialog 的 BUTTON_POSITIVE

android - 事件向上或向下传播

android - 反向地理编码 - 服务不可用 - 重启设备没有明确解决 - 批评我的手动反向地理编码

java - 如何将焦点设置在 editText Preference 上并自动显示键盘?

java - 如何获取我们自定义的AlertDialog的值?

android - AlertDialog 内的 DatePickerDialog(在特定的 EditText 上设置日期)

android - 在自定义 ListAdapter 类中显示 AlertDialog

java - 从列表中清除应用程序缓存

android - 为什么即使 fragment 不在前台也会调用 fragment 的 onStart 和 onResume 方法

android - 没有电缆的 ADB tcpip 命令