เพื่อนร่วมงานของผมและผมเถียงกันอยู่นะ เรากำลังแปลโปรแกรมที่จะ MVVM+ผูกมัดหรอ. มันต้องการจะเริ่มเป็นกล่องโต้ตอบจากเป็นชิ้นส่วน ฉันทำมันเหมือนนี้:
อยู่ในชิ้นส่วน:
fragmentBinding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// there's a bunch of code here
alertDialog.show();
}});
เป็นเพื่อนร่วมงานคนยังอ้างว่าคุณต้องทำอย่างนี้:
ในแฟ้ม xml เราเขียนตาม:
android:onClick="@{v -> viewModel.showDialog(context)}"
และใน ViewModel:
fun showDialog(context: Context){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// there's a bunch of code here
alertDialog.show();
}
ได้โปรดบอกฉันซึ่งหนึ่งคือคำตอบที่ถูกต้อง?