Android Studio AlertDialog超链接不可单击

4dbbbstv  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(183)

我有一个包含超链接的警报对话框,但该超链接不可单击。它显示为超链接

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

vkc1a9a2

vkc1a9a21#

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>

然后必须在TextView上调用setMovementMethod(LinkMovementMethod.getInstance())

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")
termsDialog.setView(termsView)
termsMessage.setText(termsMsg)
termsMessage.setMovementMethod(LinkMovementMethod.getInstance())
termsDialog.show()

相关问题