为什么数据没有从 xml 传递到 Activity?

Why data not pass from xml to Activity?

在我的 android 应用中:

 dataBinding {
     enabled = true
 }

在我的 activity:

class RegistrationActivity : RootActivity() {
    private lateinit var viewBinding: RegistrationActivityBinding // use "registration_activity.xml"
    private lateinit var registrationViewModel: RegistrationViewModel
    private var wallet = Wallet()

    companion object {
        private val TAG = RegistrationActivity::class.java.name
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Debug.d(TAG, "onCreate: savedInstanceState = $savedInstanceState")
        viewBinding = RegistrationActivityBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)        
        viewBinding.setVariable(BR.model, wallet)
        viewBinding.executePendingBindings() 

        init()
    }

    private fun init() {
        viewBinding.buttonRegistration.setOnClickListener {
            Debug.d(TAG, "initLogic: click_button, wallet = $wallet")
        }
    }
}

这是我的模型:

class Wallet() : Serializable {
    var email: String? = null
    var password: String? = null

    override fun toString(): String {
        return "\nWallet(email = $email, password = $password)"
    }
}

在xml中:

<data>

    <variable
        name="model"
        type="com.myproject.model.Wallet" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/common_color_bg">

    <include
        android:id="@+id/registrationToolbar"
        layout="@layout/tool_bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:title="@{@string/registration}" />

    <EditText
        android:id="@+id/editTextEmail"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/default_margin"
        android:hint="@string/email"
        android:importantForAutofill="no"
        android:inputType="textEmailAddress"
        android:text="@{model.email}"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/registrationToolbar" />

    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/default_margin"
        android:hint="@string/password"
        android:importantForAutofill="no"
        android:inputType="textPassword"
        android:text="@{model.password}"
        app:layout_constraintEnd_toEndOf="@+id/editTextEmail"
        app:layout_constraintStart_toStartOf="@+id/editTextEmail"
        app:layout_constraintTop_toBottomOf="@+id/editTextEmail" />

    <EditText
        android:id="@+id/editTextPasswordRetype"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/default_margin"
        android:hint="@string/retypePassword"
        android:importantForAutofill="no"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="@+id/editTextEmail"
        app:layout_constraintStart_toStartOf="@+id/editTextEmail"
        app:layout_constraintTop_toBottomOf="@+id/editTextPassword" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/buttonRegistration"
        android:layout_width="0dp"
        android:layout_height="@dimen/button_height"
        android:layout_margin="@dimen/default_margin"
        android:layout_marginBottom="@dimen/default_margin"
        android:text="@string/registration"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextPasswordRetype" />

    <include
        android:id="@+id/progressBarLayout"
        layout="@layout/progress_bar_layout"
        android:visibility="gone" />

</androidx.constraintlayout.widget.ConstraintLayout>

我在 editTextEmail 中输入了一些 email 并单击按钮。结果调用方法:

viewBinding.buttonRegistration.setOnClickListener

不错。

但是这里记录:

click_button, wallet = Wallet(email = null, password = null)

如您所见,电子邮件是 null

但我需要将电子邮件从 xml 传递给 activity。 我使用视图绑定。为什么电子邮件不通过此传递给 activity:

android:text="@{model.email}"

?

您必须使用两种方式绑定将数据从 View 返回到 ViewModel。使用 @= 在您的 xml 中启用双向数据绑定,例如:

android:text="@={model.email}"