【问题标题】:Android - Hyperlink in AlertDialogAndroid - AlertDialog 中的超链接
【发布时间】:2016-03-15 22:52:31
【问题描述】:

我想在我的 AlertDialog 中显示一个链接,但多次尝试后它仍然显示为纯文本。

我已尝试以下链接中的建议: How can I get clickable hyperlinks in AlertDialog from a string resource?

Android link in dialog

这是我目前的状态:

     final TextView msg = new TextView(context);
    final SpannableString s = new SpannableString("https://play.google.com/store/apps/details?id=com.google.zxing.client.android");
    Linkify.addLinks(s, Linkify.WEB_URLS);
    msg.setText("BradcodeScanner app must be installed on your phone\n" +
               "click on the link below.\n\n" +s);
    msg.setMovementMethod(LinkMovementMethod.getInstance());

    info_img = (ImageView) findViewById(R.id.qr_info);
    info_img.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            new AlertDialog.Builder(context)
                    .setTitle("QR Location Scan")
                    .setView(msg)
                            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    return;
                                }
                            })
                                    .setIcon(android.R.drawable.ic_dialog_info)
                                    .show();
        }
    });

【问题讨论】:

  • 你应该试试 ClickableSpan。试试this

标签: android android-alertdialog


【解决方案1】:

超链接仅适用于 HTML 文本。所以你必须:

msg.setText(Html.fromHtml("BradcodeScanner app must be installed on your phone</br><a href=\"" + s + "\">click here</a>");

【讨论】:

    【解决方案2】:

    你的意思是这样的吗?

    mTvContent.setPaintFlags(mTvContent.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    mTvContent.setTextColor(ContextCompat.getColor(this, R.color.aviary_accent_blue));
    mTvContent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (link.contains("http://")) {
            Intent i = new Intent();
            i.setAction(Intent.ACTION_VIEW);
            i.setData(Uri.parse(link));
            context.startActivity(i);
        } else {
            link = "http://" + link;
            Intent i = new Intent();
            i.setAction(Intent.ACTION_VIEW);
            i.setData(Uri.parse(link));
            context.startActivity(i);
        }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      • 2016-06-01
      • 2015-06-23
      • 2016-12-13
      相关资源
      最近更新 更多