我试图在我的对话框中添加一个超链接:
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的链接的空白对话框,而不会在这里得到任何应该出现在链接前面的文本。
我怎么才能解决这个问题?我应该两者兼得。
发布于 2017-07-11 05:57:28
您需要正确地修改对话框代码。
此代码将进入您在Create上的活动中。
@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>这里,它将向您展示超链接内对话框。

https://stackoverflow.com/questions/45023640
复制相似问题