Android Databindingで他のViewから値をとる
Sep 7, 2018 · 3 min read
例えば次のようなxmlになります.
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
> <data>
<variable
name="viewModel"
type="example.MyViewModel"/>
</data> <android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
> <android.support.design.widget.TextInputEditText
android:id="@+id/edit_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<Button
android:id="@+id/button_ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:onClick="@{(v) -> viewModel.action(editName.getText().toString())}"
app:layout_constraintTop_toBottomOf="@+id/edit_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</android.support.constraint.ConstraintLayout>
</layout>
ポイント
- Java or Kotlin側で使うようにCamelCaseでviewのIDを書く
