Android Databindingで他のViewから値をとる

yuzumone
yuzumone
Sep 7, 2018 · 3 min read
“black computer monitor” by Henri L. on Unsplash

例えば次のような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を書く
yuzumone

Written by

yuzumone

Interests: Software Engineering, Network Technology, Android, Disney and Pokemon. The source code in the stories is licensed under a SUSHI-WARE LICENSE.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade