как изменить текст виджета в настраиваемом диалоге оповещения из фрагмента - PullRequest
0 голосов
/ 03 апреля 2020

, поэтому я пытаюсь создать собственный диалог загрузки, подобный этому

enter image description here

, а вот класс

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>

вместо жесткого кода «Пожалуйста, подождите ....», я хочу установить пользовательское сообщение о загрузке из фрагмента / действия

в моем фрагмент, я хочу установить свое собственное сообщение как это

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

, но я не знаю, как получить доступ к виджету из моего класса Alert Dialog.

java или kotlin в порядке

Ответы [ 2 ]

0 голосов
/ 03 апреля 2020
fun show(yourMessage: String) {
    dialogView.textView_message.text = yourMessage
    dialog.show()
}

вы можете получить доступ к вашему textView в функции show и установить переданный параметр String в качестве его текста

0 голосов
/ 03 апреля 2020

Сначала инициализируйте textview_message в LoadingDialog классе,

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

}

Затем получите доступ к нему в Fragment классе,

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