AlertDialog中字符串资源中的超链接

时间:2016-01-28 10:52:04

标签: android hyperlink

我有一个很大的string,我需要添加2个超链接。我做到了,但它不可点击。

字符串:

<string name="freeasa"><![CDATA[<b><font color=#cc0022>text<font color=#2266bb> text </font></b> <br> text <a href=\'http://google.com\'>navigate to google.com</a><br><b><font color=#2266bb> text</font><font color=#cc0022> text </font></b><br> text <a href=\'http://yahoo.com\'> yahoo link<\a> ]]></string>

AlertDialog:

        final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle(getResources().getString(R.string.add_info));
        builder.setMessage(Html.fromHtml(getResources().getString(R.string.freeasa)))
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                }).show();

有超链接,但它们不可点击。怎么解决? enter image description here

2 个答案:

答案 0 :(得分:2)

尝试下面的自定义提醒对话框

enter image description here

LayoutInflater li = LayoutInflater.from(MainActivity.this);
        View promptsView = li.inflate(R.layout.prompts, null);

        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setView(promptsView);
        final TextView userInput = (TextView) promptsView
                .findViewById(R.id.textView);
        userInput.setMovementMethod(LinkMovementMethod.getInstance());
        userInput.setText(Html.fromHtml(getResources().getString(R.string.about_body)));
        builder.setTitle(getResources().getString(R.string.app_name));
        // builder.setMessage(Html.fromHtml(getResources().getString(R.string.about_body)))
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        }).show();

prompts.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dip">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

确保您的字符串正确且链接正常工作

答案 1 :(得分:0)

使用它来设置TextView的超链接:

TextView website = new TextView(this);
website.setText("http://www.google.com");
Linkify.addLinks(website, Linkify.ALL);

希望有所帮助