activity_main.xml中
<data class="LoginDataBinding">
<variable
name="login"
type="com.example.itstym.logina.LoginViewModel" />
<variable
name="ModelClickListener"
type="com.example.itstym.logina.interface.ClickListener" />
</data>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textStyle="bold"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome, Back"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="24dp"/>
<EditText
app:error="@{login.errorEmail}"
android:hint="@{login.emailHint}"
android:text="@{login.userEmailAddress}"
android:id="@+id/user_email_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ems="10"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/textView"
android:layout_marginStart="8dp"/>
<EditText
app:error="@{login.errorPassword}"
android:text="@{login.userPassword}"
android:hint="@{login.passwordHint}"
android:id="@+id/user_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/user_email_address"/>
<Button
android:onClick="@{(v)->ModelClickListener.onSubmitButtonClick()}"
android:text="@{login.loginButtonText}"
android:id="@+id/login_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/user_password"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class SampleActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LoginDataBinding loginDataBinding= DataBindingUtil.setContentView(this,R.layout.activity_main);
LoginViewModel viewModel = new LoginViewModel("Enter Email Address","Enter password","Login");
loginDataBinding.setLogin(viewModel);
loginDataBinding.setModelClickListener(new ClickListener() {
@Override
public void onClick() {
Toast.makeText(getApplicationContext(), loginDataBinding.getLogin().getUserEmailAddress(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), loginDataBinding.getLogin().getUserPassword(), Toast.LENGTH_SHORT).show();
}
});
}
}
但我收到setModelClickListener()
的错误未解析引用,但setLogin()工作正常。
还有一个问题是DataBindingUtil.setContentView(this,R.layout.activity_main);
返回LoginDataBinding而不是MainActivityBinding?