android {
compileSdkVersion 28
defaultConfig {
applicationId “com.example.myapplication”
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName “1.0”
testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
}
}
dataBinding {
enabled true
}
}
2、修改布局文件为DataBinding布局。
方法是:双击选中布局文件最外层布局,同时按住Alt+回车==>转换为Databinding布局。修改成功后,AS会自动生成对应的Databinding类,对应规则为test_main.xml --> TestMainBinding。
3、数据绑定:我们先要创建UserInfo实体类,再修改test_main.xml文件。数据绑定又包括单向和双向,单向绑定即当UserInfo数据发生改变时,控件(demo中的login_time_tv)会自动更新数据;双向绑定常用于输入框、ListView删除等View视图发生改变时,控件中对应的数据也需发生变化的情况。
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import com.android.databinding.library.baseAdapters.BR;
public class UserInfo extends BaseObservable {
private String pwd;
private String name;
private String loginTime;
public UserInfo(String pwd, String name) {
this.pwd = pwd;
this.name = name;
}
@Bindable
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
notifyPropertyChanged(BR.pwd);
}
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}
@Bindable
public String getLoginTime() {
return loginTime;
}
public void setLoginTime(String loginTime) {
this.loginTime = loginTime;
notifyPropertyChanged(BR.loginTime);
}
}
<?xml version="1.0" encoding="utf-8"?><layout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”>
<variable
name=“user”
type=“com.example.myapplication.UserInfo” />
<variable
name=“Activity”
type=“com.example.myapplication.MainActivity” />
<LinearLayout
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:id="@+id/container"
tools:context=".MainActivity"
android:orientation=“vertical”
android:gravity=“center_horizontal|top”>
<EditText
android:id="@+id/name_et"
android:layout_width=“200dp”
android:layout_height=“wrap_content”
android:layout_marginTop=“100dp”
android:singleLine=“true”
android:text="@={user.name}"
android:hint=“用户名”/>
<EditText
android:id="@+id/pwd_et"
android:layout_width=“200dp”
android:layout_height=“wrap_content”
android:singleLine=“true”
android:text="@={user.pwd}"
android:inputType=“numberPassword”
android:hint=“密码”/>
<TextView
android:id="@+id/login_time_tv"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text="@{user.loginTime}"/>
<Button
尾声
评论里面有些同学有疑问关于如何学习material design控件,我的建议是去GitHub搜,有很多同行给的例子,这些栗子足够入门。
有朋友说要是动真格的话,需要NDK以及JVM等的知识,首现**NDK并不是神秘的东西,**你跟着官方的步骤走一遍就知道什么回事了,无非就是一些代码格式以及原生/JAVA内存交互,进阶一点的有原生/JAVA线程交互,线程交互确实有点蛋疼,但平常避免用就好了,再说对于初学者来说关心NDK干嘛,据鄙人以前的经历,只在音视频通信和一个嵌入式信号处理(离线)的两个项目中用过,嵌入式信号处理是JAVA->NDK->.SO->MATLAB这样调用的我原来MATLAB的代码,其他的大多就用在游戏上了吧,一般的互联网公司会有人给你公司的SO包的。
至于JVM,该掌握的那部分,相信我,你会掌握的,不该你掌握的,有那些专门研究JVM的人来做,不如省省心有空看看计算机系统,编译原理。
一句话,平常多写多练,这是最基本的程序员的素质,尽量挤时间,读理论基础书籍,JVM不是未来30年唯一的虚拟机,JAVA也不一定再风靡未来30年工业界,其他的系统和语言也会雨后春笋冒出来,但你理论扎实会让你很快理解学会一个语言或者框架,你平常写的多会让你很快熟练的将新学的东西应用到实际中。
初学者,一句话,多练。