问题
I have xml with android data binding:
<?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>
and CreateUserPresenter.class
:
public class CreateUserPresenter {
public CreateUserPresenter () {}
public void createUser(String firstName, String lastName) {
}
}
How to pass the input (firstNameInput
and secondNameInput
) values in the method createUser()
on click button?
android:onClick="@{(view) -> presenter.createUser()}"
回答1:
It is a best practice to follow MVVM when you are working with DataBinding, however with your code also you can pass string values.
Try with this
android:onClick="@{(view) -> presenter.createUser(firstNameInput.getEditText().getText().toString(),secondNameInput.getEditText().getText().toString())}"
来源:https://stackoverflow.com/questions/40995071/get-values-other-views-when-onclick-databinding