【发布时间】:2021-12-31 15:50:13
【问题描述】:
我的同事和我发生了争执。 我们正在将应用程序转换为 MVVM+Binding。 需要从片段开始对话。 我是这样做的:
在片段中:
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();
}
请告诉我,哪个是正确的?
【问题讨论】: