【问题标题】:Android link in dialog对话框中的 Android 链接
【发布时间】:2012-04-10 19:42:43
【问题描述】:

如何在对话消息中添加链接?

我尝试了以下代码,但链接在 onclick 上没有任何作用:

builder.setMessage(Html.fromHtml(
      "Click on the  " +
      "<a href=\"http:\\www.google.com\">link</a> " +
       "to download."));

它也不适用于//www.google.com

【问题讨论】:

    标签: android


    【解决方案1】:

    使用以下代码转换您的 HTML 标记(包括可点击的超链接):

    ((TextView) new AlertDialog.Builder(this)
        .setTitle("Info")
        .setIcon(android.R.drawable.ic_dialog_info)
        .setMessage(Html.fromHtml("<p>Sample text, <a href=\"http://google.nl\">hyperlink</a>.</p>"))
        .show()
        // Need to be called after show(), in order to generate hyperlinks
        .findViewById(android.R.id.message))
        .setMovementMethod(LinkMovementMethod.getInstance());
    

    【讨论】:

    • qq meer,enige 遇到了 punten。 gg
    • 简单完美的答案!
    • @DIFORT 我知道,坏人会使用第三方库
    • @erdomester 您应该考虑将其标记为已接受的答案,以便为其他有相同问题的人提供更简单的解决方案。
    【解决方案2】:

    你需要使用 Linkify

    final SpannableString m = new SpannableString(message);
    Linkify.addLinks(m, Linkify.WEB_URLS);
    
        aDialog = new AlertDialog.Builder(getActivity())
                .setTitle(title)
                .setMessage(m)
                .setNeutralButton(R.string.ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            doNeutralClick();
                        }
                    }
                )
                .create();
    
        aDialog.show();
    
        ((TextView) aDialog.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
    

    this question

    【讨论】:

      【解决方案3】:

      对话框的标准实现不允许您做这些花哨的事情。您需要通过为其设置自定义布局内容来创建自定义对话框。例如:

      Dialog dialog = new Dialog(context);
      dialog.setContentView(R.layout.layout_dialog);
      

      更多详情,请阅读thisthis

      【讨论】:

        猜你喜欢
        • 2012-10-23
        • 2014-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多