我有带有android数据绑定的xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="presenter" type="com.myapp.presentation.presenter.CreateUserPresenter"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="@+id/firstNameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingTop="8dp"
android:paddingRight="16dp"
android:paddingBottom="8dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapSentences"
android:hint="@string/first_name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/secondNameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingTop="8dp"
android:paddingRight="16dp"
android:paddingBottom="8dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapSentences"
android:hint="@string/second_name" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btnCreate"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text="@string/create"
android:onClick="@{(view) -> presenter.createUser()}" />
</LinearLayout>
</layout>
和CreateUserPresenter.class
:
public class CreateUserPresenter {
public CreateUserPresenter () {}
public void createUser(String firstName, String lastName) {
}
}
如何在点击按钮上的方法firstNameInput
中传递输入(secondNameInput
和createUser()
)值?
android:onClick="@{(view) -> presenter.createUser()}"
答案 0 :(得分:4)
最佳做法是在使用DataBinding时遵循MVVM,但是使用代码也可以传递字符串值。
试试这个
android:onClick="@{(view) -> presenter.createUser(firstNameInput.getEditText().getText().toString(),secondNameInput.getEditText().getText().toString())}"