2016-12-21 2 views
2

Android에서 DataBinding을 사용하려고합니다. 하지만 내 마음 속에 질문이 있습니다.은 XML보기에서 바인딩 이벤트에 매개 변수를 보낼 수 있습니다.

xml의 바인딩 함수에 edittext의 텍스트를 보내려고합니다. 나는 의미한다; 로그인 버튼을 클릭하면 xml ID에서 현재 사용자 이름과 비밀번호를 가져 오려고합니다. 가능한가? 여기

내가 원하는 :

android:onClick="@{() -> login.onLogin(login_edt_username.text, login_edt_password.text)}" 

내 현재 사용 :

코드 :

@Override 
protected void onStart() { 
    super.onStart(); 
    binding= DataBindingUtil.setContentView(this,R.layout.activity_login); 
} 

@Override 
public void onLogin() { 
    login(binding.loginEdtUserName.getText(),binding.loginEdtPassword.getText()); 
} 

XML을 :

<layout> 

<data> 

    <variable 
     name="login" 
     type="interfaces.login.LoginInterface" /> 

</data> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_login" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.test.friends.LoginActivity"> 

    <EditText 
     android:id="@+id/login_edt_user_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Username" /> 

    <EditText 
     android:id="@+id/login_edt_password" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/login_edt_user_name" 
     android:hint="Password" 
     android:inputType="textPassword" /> 

    <Button 
     android:id="@+id/login_btn_sign_in" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:onClick="@{() -> login.onLogin()}" 
     android:layout_below="@+id/login_edt_password" 
     android:text="SIGN IN" /> 

    <Button 
     android:id="@+id/login_btn_register" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:onClick="@{() -> login.onRegister()}" 
     android:layout_below="@+id/login_btn_sign_in" 
     android:text="REGISTER" /> 

</RelativeLayout> 

미리 감사드립니다.

답변

2

이것은 양방향 데이터 바인딩 (Gradle Plugin 버전 2.1부터 사용 가능)을 사용하여 해결할 수 있습니다.

대안 1 :

<data ...> 
    <import type="android.view.View"/> 
</data> 

그런 다음, 당신이 직접 XML에 조회수의 some 속성을 참조 할 수 있습니다 : 당신의 XML에

가져 오기 android.view.View. Lambda Expressions에서 사용하는 것처럼 Java에서와 같이 Views를 사용할 수도 있습니다. 구체적 :

android:onClick="@{() -> login.onLogin(login_edt_username.getText().toString(), login_edt_password.getText().toString())}" 

대안 2 : POJO 사용. 당신이 할 수있는 당신의 XML

<data> 
    <variable 
    name="login" 
    type="interfaces.login.LoginInterface" /> 
    <variable 
    name="credentials" 
    type="models.login.Credentials" /> 
</data> 

을이 모델을 선언 한 후,

public class Credentials { 
    public String username; 
    public String password; 
} 

등 사용자의 자격 증명 정보를 깁스 POJO를 감안할 때 :

<!-- .... --> 
    <EditText 
     android:id="@+id/login_edt_user_name" 
     android:layout_width="match_parent" 
     <!-- This binds the value of the Edittext to the Model. Note the equals sign! --> 
     android:text="@={credentials.username}" /> 

    <EditText 
     android:id="@+id/login_edt_password" 
     <!-- Same here --> 
     android:text="@={credentials.password}" /> 

    <!-- ... --> 

을 마지막으로, 당신에 맞게 요구 사항

android:onClick="@{() -> login.onLogin(credentials.username, credentials.password)}" 
+0

답변 해 주셔서 감사합니다. 나는 당신의 첫 번째 대안을 시도하고 그것은 작은 변화와 함께 일했습니다. 먼저 android.view.View 데이터를 가져 와서 onClick : android : onClick = "@ {() -> login.onLogin (loginEdtUserName.getText(). toString(), loginEdtPassword.getText()에이 코드를 추가했습니다. toString())} ". –

+0

왜'View'를 가져올 필요가 있습니까? – tynn

+0

@tynn 이렇게하면 'Edittext'와 같이 View에서 상속받은 Elements를 참조 할 수 있습니다. Android에서 데이터 바인딩을 사용할 수있는 코드 생성이 필요합니다.이 부분을 확인하십시오. (데이터 바인딩 문서의 일부) (https://developer.android.com/topic/libraries/data-binding/index.html#layout_details) –

1

뷰에 ID를 추가하면 바인딩에서이 ID로이 뷰에 액세스 할 수 있습니다. 여기에는 레이아웃 자체에서 설정된 바인딩 내에서 액세스하는 것이 포함됩니다.

<EditText 
    android:id="@+id/login_edt_user_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Username" /> 

<EditText 
    android:id="@+id/login_edt_password" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/login_edt_user_name" 
    android:hint="Password" 
    android:inputType="textPassword" /> 

<Button 
    android:id="@+id/login_btn_sign_in" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:onClick="@{() -> login.onLogin(loginEdtUserName.getText(), loginEdtPassword.getText())}" 
    android:layout_below="@+id/login_edt_password" 
    android:text="SIGN IN" /> 
+0

답변 해 주셔서 감사합니다. 그것은 가져 오기보기와 함께 작동합니다. –