如何从片段更改自定义警报对话框中的小部件文本

how to change widget text in a custom alert dialog from fragment

所以我尝试制作一个像这样的自定义加载对话框

这里是 class

class LoadingDialog(mActivity: Activity) {

    private var dialog: AlertDialog
    private var dialogView : View

    init {

        val builder = AlertDialog.Builder(mActivity)
        val inflater = mActivity.layoutInflater
        dialogView = inflater.inflate(R.layout.dialog_loading,null)
        builder.setView(dialogView)
        builder.setCancelable(false)

        dialog = builder.create()

    }

    fun show() {
        dialog.show()
    }

    fun dismiss() {
        dialog.dismiss()
    }

}

这里是加载对话框xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent" android:layout_height="wrap_content">

    <ProgressBar
            android:id="@+id/progressBar2"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    <TextView
            android:id="@+id/textView_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:text="Please Wait"
            app:layout_constraintBottom_toBottomOf="@+id/progressBar2"
            app:layout_constraintStart_toEndOf="@+id/progressBar2"
            app:layout_constraintTop_toTopOf="@+id/progressBar2" />
</androidx.constraintlayout.widget.ConstraintLayout>

而不是硬代码 "Please Wait ....",我想设置来自 fragment/activity

的自定义加载消息

在我的片段中,我想像这样设置我的自定义消息

val loadingDialog = LoadingDialog(mActivity)
loadingDialog.show("custom message in here")

但我不知道如何从我的警报对话框访问小部件 class。

java 或者 kotlin 都可以

首先初始化textview_message中的LoadingDialogclass,

class LoadingDialog(mActivity: Activity) {

    private var dialog: AlertDialog
    var messageTextView: TextView // <----
    private var dialogView : View

    init {

        val builder = AlertDialog.Builder(mActivity)
        val inflater = mActivity.layoutInflater
        dialogView = inflater.inflate(R.layout.dialog_loading,null)
        messageTextView = dialogView.findViewById( R.id.textView_message ) // <---
        builder.setView(dialogView)
        builder.setCancelable(false)

        dialog = builder.create()

    }

    fun show() {
        dialog.show()
    }

    fun dismiss() {
        dialog.dismiss()
    }

}

然后在Fragmentclass,

中访问
val loadingDialog = LoadingDialog(mActivity)
val messageTextView = loadingDialog.messageTextView
loadingDialog.show("custom message in here")
fun show(yourMessage: String) {
    dialogView.textView_message.text = yourMessage
    dialog.show()
}

您可以像这样在 show 函数中访问您的 textView,并将传递的 String 参数设置为其文本