AlertDialog的超链接不能点击

1 人关注

我有一个包含超链接的提示对话框,但是超链接不能点击。它确实显示为一个超链接

    val termsDialog = AlertDialog.Builder(this@MainActivity)
    val termsView = layoutInflater.inflate(R.layout.termsdialog, null)
    val termsBox: CheckBox = termsView.findViewById(R.id.termsCheckbox)
    val termsMsg = Html.fromHtml("By using this application you accept to our Terms and Conditions and Privacy Policy. \nMore information <a href=\"https://mylink.com\">here</a>")
    termsDialog.setTitle("Terms and Conditions")
    termsDialog.setView(termsView)
    termsDialog.setMessage(termsMsg)
    termsDialog.setPositiveButton("OK") { _, _ -> }
    termsDialog.setCancelable(false)
    termsBox.setOnCheckedChangeListener { compoundButton, _ ->
        if (compoundButton.isChecked) {
            storeDialogStatus(true)
        } else {
            storeDialogStatus(false)
    // Automatic show terms dialog when dialog status is not checked
    if (!this.getDialogStatus()) {
        termsDialog.show()
    
2 个评论
是的,确实如此。它表明了这一点。你不能对这样一个盒子的标题有更多的期待。它不是一个WebView。使用WebView或其他能够捕捉到点击的东西。你不能把那个html字符串放在任何TextView上,但是TextView不会处理这个链接。但是......你可以捕捉到点击。
是的,我做了这个,现在工作得很好。谢谢
android
android-studio
kotlin
mohammed_sajid
mohammed_sajid
发布于 2022-10-24
1 个回答
Behnam Maboudi
Behnam Maboudi
发布于 2022-10-24
已采纳
0 人赞同

termsdialog.xml 中为你的信息创建一个TextView。

<?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">
    <TextView
        android:id="@+id/dialog_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Then you must call setMovementMethod(LinkMovementMethod.getInstance()) on your TextView:

val termsDialog = AlertDialog.Builder(context)
val termsView = layoutInflater.inflate(R.layout.termsdialog, null)
val termsMessage: TextView = termsView.findViewById(R.id.dialog_textview)
val termsMsg = Html.fromHtml("By using this application you accept to our Terms and Conditions and Privacy Policy. \nMore information <a href=\"https://mylink.com\">here</a>")
termsDialog.setTitle("Terms and Conditions")