Html.fromHtml无法正常工作?

时间:2017-07-11 00:43:46

标签: java android html xml android-layout

我正在尝试在我的对话框中添加超链接:

   TextView text = (TextView) layout.findViewById(R.id.text);
        text.setMovementMethod(LinkMovementMethod.getInstance());
        text.setText(Html.fromHtml("text here <i><a href=https://website.com> Terms and Conditions</a> </i> "));

            AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.DialogStyle);
            builder.setView(layout)
                    .setTitle("Dialog")

            .show();
        }

视图如下:

<TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#FFF"
    />

然而,当我这样做时,我只会得到一个空白对话框,其中包含指向website.com的链接,并且不会在链接之前显示任何文本

我该如何解决这个问题?我应该得到两个。

1 个答案:

答案 0 :(得分:0)

您需要正确修改 Dialog 代码。

此代码将在活动 上创建

@SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;


        Dialog dialogWeb = new Dialog(mContext);
        dialogWeb.setCancelable(true);
        dialogWeb.setCanceledOnTouchOutside(true);
        dialogWeb.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogWeb.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialogWeb.setContentView(R.layout.test10);

        TextView text = (TextView) dialogWeb.findViewById(R.id.text);
        text.setMovementMethod(LinkMovementMethod.getInstance());
        text.setText(Html.fromHtml("text here <i><a href=https://www.google.com> Terms and Conditions</a> </i> "));

        dialogWeb.show();


    }

这是包含文本视图的XML。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Testtinggg"
        android:textAllCaps="false"
        android:textColor="@android:color/black"
        android:textSize="22sp" />

</RelativeLayout>

此处将显示对话框中的超级链接

enter image description here