绑定不起作用 - 如何?
Posted
技术标签:
【中文标题】绑定不起作用 - 如何?【英文标题】:Binding not working - how to? 【发布时间】:2017-12-13 19:01:41 【问题描述】:我不明白为什么我的 java bean 属性在用户更改某些文本字段中的文本后没有更新。我已经配置了 pojo 和绑定,但它不起作用。有人可以解释我为什么不工作吗? 如果我使用 setter 在 onCreate 方法中初始化 bean,则绑定只是在开始时工作。但是当我调用 onSignup 方法时,更改不会合并到用户 bean 中。
activity_registration.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="main.client.android.musichall.registration.model.User" />
</data>
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:padding="@dimen/activity_horizontal_margin"
tools:context="main.client.android.musichall.registration.RegistrationActivity">
<Spinner
android:id="@+id/spinner"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@array/activity_registration_modes" />
<TextView
android:id="@+id/activity_registration_text_name"
android:layout_
android:layout_
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/spinner"
tools:layout_editor_absoluteX="8dp"
android:text="@string/activity_registration_text_name" />
<EditText
android:id="@+id/activity_registration_text_field_name"
android:layout_
android:layout_
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintTop_toBottomOf="@+id/activity_registration_text_name"
tools:layout_editor_absoluteX="8dp"
android:text="@user.name"/>
<TextView
android:id="@+id/activity_registration_text_email"
android:layout_
android:layout_
android:layout_marginTop="8dp"
android:text="@string/activity_registration_text_email"
app:layout_constraintTop_toBottomOf="@+id/activity_registration_text_field_name"
tools:layout_editor_absoluteX="8dp" />
<EditText
android:id="@+id/activity_registration_text_field_email"
android:layout_
android:layout_
android:layout_marginTop="12dp"
android:ems="10"
android:inputType="textEmailAddress"
app:layout_constraintTop_toBottomOf="@+id/activity_registration_text_email"
tools:layout_editor_absoluteX="8dp"
android:text="@user.email"/>
<TextView
android:id="@+id/activity_registration_text_password"
android:layout_
android:layout_
android:layout_marginTop="8dp"
android:text="@string/activity_registration_text_password"
app:layout_constraintTop_toBottomOf="@+id/activity_registration_text_field_email"
tools:layout_editor_absoluteX="8dp" />
<EditText
android:id="@+id/activity_registration_text_field_password"
android:layout_
android:layout_
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPassword"
android:text="@user.password"
app:layout_constraintTop_toBottomOf="@+id/activity_registration_text_password"
tools:layout_editor_absoluteX="8dp" />
<Button
android:id="@+id/activity_registration_button_sign_up"
android:layout_
android:layout_
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="@string/sign_up"
android:onClick="onSignUpClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
</layout>
RegistrationActivity.java
package main.client.android.musichall.registration;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import main.client.android.musichall.R;
import main.client.android.musichall.databinding.ActivityRegistrationBinding;
import main.client.android.musichall.login.LoginActivity;
import main.client.android.musichall.registration.model.User;
public class RegistrationActivity extends AppCompatActivity
private static RegistrationPresenter presenter;
private ActivityRegistrationBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
presenter = new RegistrationPresenter(this);
binding = DataBindingUtil.setContentView(this, R.layout.activity_registration);
User user = new User();
binding.setUser(user);
public void onSignUpClick (View view)
User user = binding.getUser();
user.getEmail();
presenter.onSignUpClick(user);
public void onSignUpDone(User user)
startActivity(new Intent(this, LoginActivity.class));
用户类
package main.client.android.musichall.registration.model;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import main.client.android.musichall.BR;
/**
* Created by sangra on 02/12/17.
*/
public class User extends BaseObservable
private String name;
private String username;
private String email;
private String password;
@Bindable
public String getName()
return name;
public void setName(String name)
this.name = name;
notifyPropertyChanged(BR.name);
@Bindable
public String getUsername()
return username;
public void setUsername(String username)
this.username = username;
notifyPropertyChanged(BR.username);
@Bindable
public String getEmail()
return email;
public void setEmail(String email)
this.email = email;
notifyPropertyChanged(BR.email);
@Bindable
public String getPassword()
return password;
public void setPassword(String password)
this.password = password;
notifyPropertyChanged(BR.password);
【问题讨论】:
【参考方案1】:您似乎没有实现双向数据绑定。因此,目前您的视图只会显示您的用户对象中的内容,并且不会更改它的值。
要实现双向数据绑定,您需要更改以下内容:
android:text="@user.password"
转android:text="@=user.password"
绑定user.password时请注意@后的=
现在在 EditText 中输入文本时,应更新 User 中的相应字段。
希望这会有所帮助。
来源:https://www.bignerdranch.com/blog/two-way-data-binding-on-android-observing-your-view-with-xml
【讨论】:
非常感谢 Rhyp,奇怪的是 android 官方文档没有提供任何相关信息或示例。以上是关于绑定不起作用 - 如何?的主要内容,如果未能解决你的问题,请参考以下文章