public void OnClickListenerBinding(User user){ Toast.makeText(MainActivity.this,user.getLastName(),Toast.LENGTH_SHORT).show(); }
<TextView android:onClick="@{() -> presenter.OnClickListenerBinding(user)}" android:text="@{User.lastName}" android:layout_width="wrap_content" android:layout_height="wrap_content" />
只有绑定监听器事件不行,别的都可以的,这是什么原因,语法检查很多遍没错啊
所以到底是User还是user呢? 建议贴全代码
public class MainActivity extends AppCompatActivity { User user = new User("小白","小黑"); private ActivityMainBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); // binding.firstName.setText(user.getFirstName()); // binding.lastName.setText(user.getLastName()); binding.setUser(user); // binding.setVariable(BR.User,user); binding.setPresenter(new Presenter()); } public class Presenter{ public void onTextChanged(CharSequence s,int start,int before,int count){ user.setFirstName(s.toString()); binding.setUser(user); } public void onClick(View view){ Toast.makeText(MainActivity.this,"点到了",Toast.LENGTH_SHORT).show(); } public void OnClickListenerBinding(User user){ Toast.makeText(MainActivity.this,user.getLastName(),Toast.LENGTH_SHORT).show(); } } }
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" > <data> <variable name="User" type="chachewuyou.qs.demo.User"/> <variable name="presenter" type="chachewuyou.qs.demo.MainActivity.Presenter"/> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical"> <EditText android:onTextChanged="@{presenter.onTextChanged}" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入First Name" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入Last Name" /> <TextView android:onClick="@{presenter.onClick}" android:text="@{User.firstName}" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:onClick="@{() -> presenter.OnClickListenerBinding(user)}" android:text="@{User.lastName}" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </layout>