所以我在做一個項目,我完全迷路了。我已經看到了如何使用textview進行數據綁定,但是有人要求我使用EditText視圖進行雙向數據綁定。到目前為止,我都是帶著它來的。
The XML File.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="myShoe"
type="com.udacity.shoestore.product.Shoe" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/title_detail_view"
style="@style/title_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="@string/add_shoe_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/shoe_name"
style="@style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:hint="@string/shoe_name_string"
android:inputType="text"
android:textSize="30sp"
android:text="@={myShoe.name}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_detail_view" />
<EditText
android:id="@+id/shoe_size"
style="@style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/size_string"
android:inputType="number|numberDecimal"
android:textSize="15sp"
android:text="@={myShoe.size}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shoe_name" />
<EditText
android:id="@+id/company_name"
style="@style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/company_string"
android:inputType="text"
android:textSize="15sp"
android:text="@={myShoe.company}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shoe_size" />
<EditText
android:id="@+id/shoe_description"
style="@style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/description_string"
android:inputType="text"
android:textSize="15sp"
android:text="@={myShoe.description}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/company_name" />
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimaryDark"
android:text="@string/cancel_string"
android:textColor="@android:color/white"
app:layout_constraintBaseline_toBaselineOf="@+id/savee_button"
app:layout_constraintEnd_toStartOf="@+id/savee_button"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/savee_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="88dp"
android:backgroundTint="@color/colorPrimaryDark"
android:text="@string/save_string"
android:textColor="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shoe_description" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
我被告知要實現它的一個片段,它應該工作。但我不知道怎么做。這是碎片
class ShoeDetailsFragment : Fragment() {
private val viewModel: ActivityViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: FragmentShoeDetailsBinding = DataBindingUtil.inflate(
inflater,
R.layout.fragment_shoe_details,
container, false
)
//initializing the button and clearing the views once canceled
binding.cancelButton.setOnClickListener { v: View ->
v.findNavController().navigateUp()
binding.shoeName.text.clear()
binding.shoeSize.text.clear()
binding.companyName.text.clear()
binding.shoeDescription.text.clear()
}
//initializing the button and saving the info to transfer to the shoeList
binding.saveeButton.setOnClickListener { v: View ->
v.findNavController().navigateUp()
val name = shoe_name.text.toString()
val size = shoe_size.text.toString()
val brand = company_name.text.toString()
val details = shoe_description.text.toString()
viewModel.addShoe(name, size, brand, details)
}
return binding.root
}
}
我愿意接受任何初始化綁定屬性的想法,這樣我就可以在布局和片段中使用它。還是我看錯了?
另外,XML文件在這個片段中表示
我想我也為我的納米學位做了這個項目,你的代碼給我留下了深刻的印象。
在我的
ViewModel
中,我為每個EditText
創建了3個變量然后我和將創建一個共享ViewModel,在
ShoeDetailsFragment
和ShoeListingFragment
之間共享數據。在SharedViewModel中
我為每個EditText創建了3個變量(這只是前2個EditText)
對于xml,我做了與您所做的完全相同的操作,但是使用了第3個變量作為雙向數據綁定
我看到您在
ShoeDetailFragment
代碼中包含了這行代碼在我的例子中,我是在SharedViewModel中做的。
使用數據綁定,我將onClick位移動到xml
所有與你的項目最好的,你也可以參考我的項目,如果需要的話
快樂的編碼伙伴