<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="com.example.cunli.databing.web.User"/> </data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.email}"
android:id="@+id/email"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.password}"
android:id="@+id/password"
android:layout_marginTop="10dp"/> <Button
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:onClick="hello"
android:text="@string/app_name"/> </LinearLayout>
</layout> package com.example.cunli.databing.web; import android.databinding.BaseObservable;
import android.databinding.Bindable; import com.example.cunli.databing.BR; /**
* Created by winder on 2016/7/6.
*/
public class User extends BaseObservable {
public String email;
public String password; public User() {
} public User(String email, String password) {
this.email = email;
this.password = password;
} @Bindable
public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
notifyPropertyChanged(BR.password);
} @Bindable
public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
notifyPropertyChanged(BR.email);
} } package com.example.cunli.databing.activity; import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast; import com.example.cunli.databing.R;
import com.example.cunli.databing.databinding.ActivityMainBinding;
import com.example.cunli.databing.server.TeachLogServer;
import com.example.cunli.databing.web.CheckTeachLog;
import com.example.cunli.databing.web.TeachLog;
import com.example.cunli.databing.web.User; public class MainActivity extends AppCompatActivity { private TeachLogServer teachLogServer = new TeachLogServer();
User user; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); user = new User("haliluya", "thankyoulord");
binding.setUser(user); } public void hello(View view){ Log.e("password","------getEmail------- "+user.getEmail()); Toast.makeText(this,user.getEmail()+"\t"+user.getPassword(), Toast.LENGTH_SHORT).show();
} }