4

我阅读了示例代码并在下面尝试了一些方法,但它不起作用。该文本似乎不是超链接。请帮忙:(

String randomString = XXXX.getString();
        if(randomString .contains("XXXX"))
        {
            TextView tv  = new TextView(this);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
            tv.setText(randomString +"/n"+Html.fromHtml("<a href=https://play.google.com/store/apps/details?id=com.xxxxxxxxx>Click Here</a>"));
            AlertDialog dialog = new AlertDialog.Builder(activity.this)
            .setView(tv)
            .setPositiveButton("OK!", new DialogInterface.OnClickListener()
            {

                public void onClick(DialogInterface dialog, int which)
                {

                }
            })
            .show();

        }

编辑:这项工作:

tv.setText(Html.fromHtml("<br><a href=https://play.google.com/store/apps/details?id=com.xxxxxxxxx>Click Here</a>"));

没有 randomString+

只要我把randomString+Html.fromHtml....“点击这里”变成常规文本

但我也想将 randomString 放在 textview 中。

4

3 回答 3

5

您的 URL 周围缺少引号。尝试:

Html.fromHtml("<a href='https://play.google.com/store/apps/details?id=com.xxxxxxxxx'>Click Here</a>")

请注意 URL 周围的单引号。

于 2012-10-09T23:14:22.797 回答
1
AlertDialog.Builder builder1 = new AlertDialog.Builder(youractivity.this);

builder1.setMessage(Html.fromHtml("your message,<a href=\"http://www.google.com\">link</a>"));

builder1.setCancelable(false);
builder1.setPositiveButton("ok", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    }
});

AlertDialog Alert1 = builder1.create();
Alert1 .show();
((TextView)Alert1.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
于 2017-08-28T13:10:28.623 回答
0

您需要以 HTML 样式使用 all

像这样

Html.fromHtml(
"<b>Hello</b><br>" +
"<a href='https://play.google.com/store/apps/details?id=com.xxxxxxxxx'>Click Here</a>"
)
于 2013-12-16T09:56:37.223 回答