How to have text and clickable URL link on Alertdi

2019-07-21 03:21发布

I read sample codes and tried something below, but it doesn't work. The text didn't appear to be a hyperlink. Please help:(

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();

        }

EDIT: This work:

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

without the randomString+

As soon as I put the randomString+Html.fromHtml.... The "Click Here" become a regular text

But I would like to put the randomString inside the textview as well.

3条回答
对你真心纯属浪费
2楼-- · 2019-07-21 03:45
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());
查看更多
Summer. ? 凉城
3楼-- · 2019-07-21 03:55

You're missing quotes around your URL. Try:

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

Note the single quotes around the URL.

查看更多
冷血范
4楼-- · 2019-07-21 04:03

You need to use all in HTML style

like this

Html.fromHtml(
"<b>Hello</b><br>" +
"<a href='https://play.google.com/store/apps/details?id=com.xxxxxxxxx'>Click Here</a>"
)
查看更多
登录 后发表回答