viewBinding { enabled = true } dataBinding { enabled = true } <?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> <import type="java.util.List"/> <import type="java.util.Map"/> <variable name="list" type="List<String>" /> <variable name="index" type="Integer" /> <variable name="user" type="com.example.day1208.User" /> <variable name="view" type="android.view.View" /> <variable name="map" type="java.util.Map<String,Object>" /> <variable name="key" type="String" /> <variable name="presenter" type="com.example.day1208.Presenter" /> <variable name="userInfo" type="com.example.day1208.UserInfo" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity2"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.name}" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{String.valueOf(user.age)}" android:visibility="@{user.age>20?view.GONE:view.VISIBLE}" /> <EditText android:id="@+id/et_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@{etText.text}" /> <TextView android:text="@{list.get(index)}" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:text="@{String.valueOf(map.get(key))}" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:text="事件绑定" android:onClick="@{()->presenter.OnSaved()}" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:text="@{userInfo.name}" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:text="@{presenter.firstName}" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:text="可观察对象" android:onClick="@{()->presenter.OnSaved1(userInfo)}" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </layout>
public class MainActivity2 extends AppCompatActivity { ActivityMain2Binding main2Binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); main2Binding = DataBindingUtil.setContentView(this,R.layout.activity_main2); User john = new User("John", 18); main2Binding.setUser(john); List<String> list = new ArrayList<>(); list.add("laowang"); list.add("jjiwef"); list.add("sdf"); main2Binding.setList(list); main2Binding.setIndex(1); Map<String,Object> map = new HashMap<>(); map.put("hhh","map:::laowang"); map.put("ttt","map:::sdfsdf"); map.put("age",20); main2Binding.setMap(map); main2Binding.setKey("age"); Presenter presenter = new Presenter("asdfsdsdf"); main2Binding.setPresenter(presenter); UserInfo userInfo = new UserInfo("dfg", 10); main2Binding.setUserInfo(userInfo); } }