简书链接:[androiddatabind双向绑定爽不爽能干什么看看这个就明白了`](https://www.jianshu.com/p/dc1656557929)
文章字数:438,阅读全文大约需要1分钟
双向绑定在公司应用开发中很少用到,就用户名可以用用双向绑定吧,
双向绑定不需要在模型里面填写notifyFielChange()
以及继承BaseObserveable```
还是非常方便的,那么我用来做什么了呢?


每次加功能新增任何一个配置,
我不需要findbyid,
我不需要修改java代码
我只需要填写一个控件
然后只需要这样app:text="@={model.videogagminute}"
注意,这里我用了一个=
就这么简单,哈哈哈,qq机器人用这个简直绝配!!! android databind来做配置修改啊,另外我的数据库自动升级util也是架构的非常好,不需要写什么升级语句浪费时间.
1 2 3 4 5 6 7 8 9
| <EditText xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="禁言分钟" android:inputType="numberSigned" android:minWidth="10dp" android:textSize="12sp" app:text="@={model.videogagminute}" />
|
这里会引出几个坑,第一 int类型 long类型 问题
第二个问题 app:text
属于自定义属性 而不是android:text
所以需要自己写的哈!
整个机器人就这样ok了,我只需要负责机器人逻辑,基本上不需要管view的填充和保存问题了,双休绑定+我的数据库工具类 一次搞定终生受益
模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
@Table("groupconfig") public class GroupWhiteNameBean extends AccountBean implements TwoDataHolder ,Cloneable{ public GroupWhiteNameBean() {
}
public GroupWhiteNameBean(String account) { super(account); }
@Override public AccountBean setAccount(String account) { super.setAccount(account); return GroupWhiteNameBean.this; }
public boolean isDisable() { return disable; }
public void setDisable(boolean disable) { this.disable = disable; }
public String getPostfix() { return postfix; }
public void setPostfix(String postfix) { this.postfix = postfix; }
private String postfix="";
public boolean isBreaklogic() { return breaklogic; }
public void setBreaklogic(boolean breaklogic) { this.breaklogic = breaklogic; }
/** * 当触发违规后逻辑是否继续执行 */ private boolean breaklogic;
public int getPicgagsecond() { return picgagsecond; }
public void setPicgagsecond(int picgagsecond) { this.picgagsecond = picgagsecond; }
private int picgagsecond = 60 * 5;
public String getPicgagsecondtip() { if (TextUtils.isEmpty(picgagsecondtip)) { return "禁止发图片哈"; } return picgagsecondtip; }
public void setPicgagsecondtip(String picgagsecondtip) { this.picgagsecondtip = picgagsecondtip; }
private String picgagsecondtip = "本群禁止发图片";
/** * 违规启用艾特回复 } //省略大量 代码
|
最后我还是弄上如何让edittext自定义非字符串类型吧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| @BindingAdapter("text") public static void setTextApp(TextView view, int value) { view.setText(Integer.toString(value)); }
// @InverseBindingAdapter(attribute = "android:text")
@InverseBindingAdapter(attribute = "android:text", event = "android:textAttrChanged") public static int getText(TextView view) { return Integer.parseInt(view.getText().toString()); }
@BindingAdapter("android:text") public static void setTextLong(TextView view, long value) { view.setText(Long.toString(value)); }
@BindingAdapter("text") public static void setTextLongApp(TextView view, long value) { setTextLong(view, value); }
@InverseBindingAdapter(attribute = "android:text", event = "android:textAttrChanged") public static long getTextLong(TextView view) { String s = view.getText().toString(); if (TextUtils.isEmpty(s)) { return 0; } else {
return Long.parseLong(s); } }
@InverseBindingAdapter(attribute = "text", event = "textAttrChanged") public static long getTextLongApp(TextView view) { return getTextLong(view); }
@InverseBindingAdapter(attribute = "text", event = "textAttrChanged") public static int getTextXX(TextView view) {
String s = view.getText().toString(); if (TextUtils.isEmpty(s)) { return 0; } {
try { return Integer.parseInt(s);
} catch (NumberFormatException e) { return 0; } } }
@InverseBindingAdapter(attribute = "text") public static int getTextFromApp(TextView view) { return getTextXX(view); }
@InverseBindingAdapter(attribute = "text") public static String getTextFromAppX(TextView view) { return view.getText().toString();// }
/**
|