标签:
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() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//将改变后的值给user对象
user.userName = s.toString();
}
};
//对password的监听
public TextWatcher passwordWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public 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() {
@Override
public 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() {
@Override
public void run() {
Toast.makeText(context,content,Toast.LENGTH_SHORT).show();
pd.dismiss();
}
});
}
}
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="event"
type="com.example.yangzhi.mvvm.UserEvent"></variable>
<variable
name="handler"
type="com.example.yangzhi.mvvm.MyHandler"></variable>
</data>
</layout>
addTextChangedListener="@{event.userNameWatcher}"
<EditText
addTextChangedListener="@{event.userNameWatcher}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
addTextChangedListener="@{event.passwordWatcher}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" />
<Button
android: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;
@Override
protected 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