DataBinding
1 <?xml version="1.0" encoding="utf-8"?> 2 <layout xmlns:android="http://schemas.android.com/apk/res/android"> 3 <data> 4 <variable name="user" type="com.example.cunli.databing.web.User"/> 5 6 </data> 7 <LinearLayout 8 android:orientation="vertical" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent"> 11 <EditText android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="@={user.email}" 14 android:id="@+id/email" 15 /> 16 <TextView android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:text="@={user.password}" 19 android:id="@+id/password" 20 android:layout_marginTop="10dp"/> 21 22 <Button 23 android:layout_marginTop="30dp" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:id="@+id/btn" 27 android:onClick="hello" 28 android:text="@string/app_name"/> 29 30 31 32 </LinearLayout> 33 </layout> 34 35 36 37 package com.example.cunli.databing.web; 38 39 import android.databinding.BaseObservable; 40 import android.databinding.Bindable; 41 42 import com.example.cunli.databing.BR; 43 44 45 /** 46 * Created by winder on 2016/7/6. 47 */ 48 public class User extends BaseObservable { 49 public String email; 50 public String password; 51 52 public User() { 53 } 54 55 public User(String email, String password) { 56 this.email = email; 57 this.password = password; 58 } 59 60 @Bindable 61 public String getPassword() { 62 return password; 63 } 64 65 public void setPassword(String password) { 66 this.password = password; 67 notifyPropertyChanged(BR.password); 68 } 69 70 @Bindable 71 public String getEmail() { 72 return email; 73 } 74 75 public void setEmail(String email) { 76 this.email = email; 77 notifyPropertyChanged(BR.email); 78 } 79 80 81 82 } 83 84 85 86 package com.example.cunli.databing.activity; 87 88 import android.databinding.DataBindingUtil; 89 import android.os.Bundle; 90 import android.support.v7.app.AppCompatActivity; 91 import android.util.Log; 92 import android.view.View; 93 import android.widget.Toast; 94 95 96 import com.example.cunli.databing.R; 97 import com.example.cunli.databing.databinding.ActivityMainBinding; 98 import com.example.cunli.databing.server.TeachLogServer; 99 import com.example.cunli.databing.web.CheckTeachLog; 100 import com.example.cunli.databing.web.TeachLog; 101 import com.example.cunli.databing.web.User; 102 103 public class MainActivity extends AppCompatActivity { 104 105 private TeachLogServer teachLogServer = new TeachLogServer(); 106 User user; 107 108 @Override 109 protected void onCreate(Bundle savedInstanceState) { 110 super.onCreate(savedInstanceState); 111 112 ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); 113 114 user = new User("haliluya", "thankyoulord"); 115 binding.setUser(user); 116 117 } 118 119 public void hello(View view){ 120 121 122 Log.e("password","------getEmail------- "+user.getEmail()); 123 124 Toast.makeText(this,user.getEmail()+"\t"+user.getPassword(), Toast.LENGTH_SHORT).show(); 125 } 126 127 128 }