标签:
MVVM:Model-代表你的基本业务逻辑 View-显示的内容 ViewModel--将Model 和 View联系起来的对象
android {dataBinding{enabled=true;}}
public class User {public String userName;public String password;}
public class UserEvent {private User user;//构造函数,将user传进来public UserEvent(User user) {this.user = user;}//对userName的监听public TextWatcher userNameWatcher = new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {//将改变后的值给user对象user.userName = s.toString();}};//对password的监听public TextWatcher passwordWatcher = new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {//将改变后的值给user对象user.password=s.toString();}};}
public class MyHandler {private User user;private Activity context;private ProgressDialog pd;//构造传参public MyHandler(User user,Activity context,ProgressDialog pd){this.user=user;this.context=context;this.pd=pd;}//登录事件public void onClickLoginUser(View v){if(TextUtils.isEmpty(user.userName) || TextUtils.isEmpty(user.password)){showToast("用户名或密码为空");}else{pd.show();new Thread(new Runnable() {@Overridepublic void run() {//判断是否登录成功UserLoginNet net=new UserLoginNet();if(net.sendUserInfo(user)){showToast("欢迎回来:"+user.userName);}else{showToast("用户名或密码错误");}}}).start();}}private void showToast(final String content){context.runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(context,content,Toast.LENGTH_SHORT).show();pd.dismiss();}});}}
<layout xmlns:android="http://schemas.android.com/apk/res/android"><data><variablename="event"type="com.example.yangzhi.mvvm.UserEvent"></variable><variablename="handler"type="com.example.yangzhi.mvvm.MyHandler"></variable></data></layout>
addTextChangedListener="@{event.userNameWatcher}"
<EditTextaddTextChangedListener="@{event.userNameWatcher}"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入用户名" /><EditTextaddTextChangedListener="@{event.passwordWatcher}"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入密码" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="@{handler.onClickLoginUser}"android:text="登录" />
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);//这个就不再需要//setContentView(R.layout.activity_main);user = new User();UserEvent event = new UserEvent(user);binding.setEvent(event);ProgressDialog pd=new ProgressDialog(this);MyHandler myHandler=new MyHandler(user,this,pd);binding.setHandler(myHandler);
public class MainActivity extends AppCompatActivity {private User user;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);//这个就不再需要//setContentView(R.layout.activity_main);user = new User();UserEvent event = new UserEvent(user);binding.setEvent(event);ProgressDialog pd=new ProgressDialog(this);MyHandler myHandler=new MyHandler(user,this,pd);binding.setHandler(myHandler);}}
Android MVVM简单配置--data Binding Library
标签:
原文地址:http://www.cnblogs.com/YoungZhi/p/5666815.html