2016-12-06 5 views
0

Ich habe xml mit Android Datenbindung:Erhalten Sie andere Ansichten bei onClick. Datenbindung

<?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> 

und CreateUserPresenter.class:

public class CreateUserPresenter { 

    public CreateUserPresenter() {} 

    public void createUser(String firstName, String lastName) { 

    } 
} 

Wie die Eingabe zu übergeben (firstNameInput und secondNameInput) Werte in der Methode createUser() auf Klick-Taste?

android:onClick="@{(view) -> presenter.createUser()}" 
+0

Check hier http://stackoverflow.com/a/37105213/1790537 – Nas

Antwort

3

Es ist eine bewährte Methode MVVM zu folgen, wenn Sie mit Databinding arbeiten jedoch mit Ihrem Code können Sie auch String-Werte übergeben.

Versuchen mit diesem

android:onClick="@{(view) -> presenter.createUser(firstNameInput.getEditText().getText().toString(),secondNameInput.getEditText().getText().toString())}" 
Verwandte Themen