在onClick时获取其他视图的值.数据绑定 [英] Get values other views when onClick. DataBinding

查看:61
本文介绍了在onClick时获取其他视图的值.数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML与Android数据绑定:

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>

CreateUserPresenter.class:

public class CreateUserPresenter {

    public CreateUserPresenter () {}

    public void createUser(String firstName, String lastName) {

    }
}

如何在单击按钮的方法createUser()中传递输入的值(firstNameInputsecondNameInput)?

How to pass the input (firstNameInput and secondNameInput) values in the method createUser() on click button?

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

推荐答案

在使用DataBinding时,遵循MVVM是最佳实践,但是,通过代码也可以传递字符串值.

It is a best practice to follow MVVM when you are working with DataBinding, however with your code also you can pass string values.

尝试一下

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

这篇关于在onClick时获取其他视图的值.数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆