Android官方DataBinding(七):BindingMethods与BindingMethod
BindingMethods与BindingMethod用于类的注解,简单的可以理解为,定义xml中定义的属性与某个medthod(方法)绑定。
同样,新建一个User数据模型,User.java:
package zhangphil.test;
import android.databinding.BaseObservable;
import android.databinding.ObservableField;
/**
* Created by Phil on 2017/8/17.
*/
public class User extends BaseObservable {
public final ObservableField<String> content = new ObservableField<>();
}
写activity_main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="zhangphil.test.Util" />
<variable
name="user"
type="zhangphil.test.User" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@={user.content}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{Util.displayPassowrdText(user.content)}" />
<zhangphil.test.PhilTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.content}"
app:zhangphiltoast="@{user.content}" />
</LinearLayout>
</layout>
package zhangphil.test;
/**
* Created by Phil on 2017/8/18.
*/
public class Util {
public static String displayPassowrdText(String text) {
if (text == null) {
return null;
}
String s = "";
for (int i = 0; i < text.length(); i++) {
s = s + "*";
}
return s;
}
}
注意PhilTextView.java,BindingMethods与BindingMethod的用法体现在该类中:
package zhangphil.test;
import android.content.Context;
import android.databinding.BindingMethod;
import android.databinding.BindingMethods;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Phil on 2017/8/22.
*/
@BindingMethods({@BindingMethod(type = TextView.class, attribute = "zhangphiltoast", method = "showZhangPhilToast")})
public class PhilTextView extends TextView {
public PhilTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public void showZhangPhilToast(String s) {
if (TextUtils.isEmpty(s)) {
return;
}
Toast.makeText(getContext(), s, Toast.LENGTH_LONG).show();
}
}
BindingMethods与BindingMethod定义了一个自己声明的属性:zhangphiltoast
该属性与PhilTextView中的showZhangPhilToast绑定。
上层MainActivity.java代码:
package zhangphil.test;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import zhangphil.test.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User();
binding.setUser(user);
}
}
代码运行结果:
每一次输入字符时候,Toast都会弹出一个Toast显示字符串。
附录:
1,《Android官方DataBinding简例(一)》链接:http://blog.csdn.net/zhangphil/article/details/77322530
2,《Android官方DataBinding(二):动态数据更新notifyPropertyChanged》链接:http://blog.csdn.net/zhangphil/article/details/77328688
3,《Android官方DataBinding(三):RecyclerView 使用ViewDataBinding更新数据》链接:http://blog.csdn.net/zhangphil/article/details/77367432
4,《Android官方DataBinding(四):BindingAdapter》链接:http://blog.csdn.net/zhangphil/article/details/77374211
5,《Android官方DataBinding(五):ObservableMap,ObservableArrayMap》链接:http://blog.csdn.net/zhangphil/article/details/77448933
6,《Android官方DataBinding(六): @= 操作符进行双向绑定》链接:http://blog.csdn.net/zhangphil/article/details/77454045