【问题标题】:Android hyperlinks on TextView in custom AlertDialog not clickable自定义AlertDialog中TextView上的Android超链接不可点击
【发布时间】:2012-08-22 09:12:03
【问题描述】:

在我之前的问题之后,我仍然无法在我的自定义 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());

显示如下:

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

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

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

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

我也尝试过使用Linkifyandroid: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 &lt;a href=\"http://www.poon-world.com\"&gt;here&lt;/a&gt; to switch on the red light.\n")); 就像图片3。链接标记为链接但不可点击。使用textView.setMovementMethod(LinkMovementMethod.getInstance());,如图 2 所示。

标签: android android-alertdialog


【解决方案1】:

您在没有 android:autoLink="all"android:clickable="true"android:linksClickable="false" 的情况下定义了 TextView

对于 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" />

【讨论】:

  • 最后。我已经尝试过Html.fromHtml,但没有成功。因为唯一的区别是您定义 TextView 的方式回到了我的 TextView 并删除了 android:autoLink="all" android:clickable="true" android:linksClickable="false" 现在它甚至可以将字符串设置为 textView.setText(R.string.link_text_manual); 非常感谢
【解决方案2】:

无论如何,您的代码运行良好,...

AlertDialog 代码:

LayoutInflater inflater = LayoutInflater.from(ctx);
View layout = inflater.inflate(R.layout.info, null);
AlertDialog.Builder about = new AlertDialog.Builder(ctx);
about.setTitle("Data");
about.setIcon(R.drawable.info);

// Make the textview clickable. Must be called after show()
TextView textView = (TextView) layout.findViewById(R.id.infoDetailText);
textView.setText(R.string.link_text_manual);
textView.setMovementMethod(LinkMovementMethod.getInstance());
about.setView(layout);
AlertDialog displayInfo = about.create();
displayInfo.show();

TextView XML 代码:

<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:padding="4dp" />

删除了两个属性..

android:autoLink="all"
android:linksClickable="true"

【讨论】:

  • 谢谢。在@rajpara 回答之后发现如此标记的是正确答案。赞成你的。我不敢相信花了多长时间。奇怪的是,删除这些属性如何使它在所有其他线程中始终被推荐时起作用......无论如何,非常感谢您的时间和努力
【解决方案3】:

试试这个

setText(Html.fromHtml(String));

【讨论】:

  • 谢谢,但textView.setText(Html.fromHtml(getString(R.string.link_text_manual)));与第二张图片完全相同。
  • 也为文本视图设置这个属性。 tv.setMovementMethod(LinkMovementMethod.getInstance());
猜你喜欢
  • 2015-05-09
  • 2012-03-13
  • 2015-04-27
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 2016-09-10
相关资源
最近更新 更多