編輯:關於 Android 程式設計
DataBinding 是 Google 官方推出的資料綁定框架,值得花時間學一下。英文夠好的話可以直接看官方文件:https://developer.android.com/topic/libraries/data-binding/index.html
在該 Module 的 build.gradle 中加上:
dataBinding {
enabled true;
}

接著 Rebuild Project,讓它產生綁定所需的類別。
然後建一個簡單的 JavaBean,Student:
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
修改 activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools">
<data>
<variable
name="student"
type="com.example.edu.databindingsimple.Student" />
</data>
<LinearLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.edu.databindingsimple.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@{String.valueOf(student.age)}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@{student.name}" />
</LinearLayout>
</layout>
幾個要點:根節點必須是 <layout>;<data> 裡 variable 的 name 可以任取,但必須與下方使用的一致;type 就是該物件所屬的類別;@{...} 裡寫的就是要顯示的值。這樣一來,Java 端就不需要再呼叫 setText() 了。
MainActivity:
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.example.edu.databindingsimple.R;
import com.example.edu.databindingsimple.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding mainBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
Student student = new Student("我不是程序員", 22);
// 兩種寫法都可以
// mainBinding.setStudent(student);
mainBinding.setVariable(BR.student, student);
}
}
第一次執行會報錯,原因是 age 是 int,而 text 屬性期望的是字串,所以前面才要用 String.valueOf() 包一層。修正後執行:

是不是清爽很多?沒有 findViewById,也沒有 setText。
修改 activity_main.xml,多宣告一個 variable 指向 MainActivity:
android:onClick="@{myclick.myClick}"
對應的 MainActivity:
public void myClick(View view) {
Toast.makeText(this, ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
另外別忘了在 onCreate 中 mainBinding.setMyclick(this);。

除了傳 View,也可以直接把資料傳進去,寫法是用 Lambda 表達式:
android:onClick="@{()->myclick.myClick(student)}"
public void myClick(Student student) {
Toast.makeText(this, "你好啊", Toast.LENGTH_SHORT).show();
}
連無參數的版本也行:
android:onClick="@{()->myclick.myClick()}"
public void myClick() {
Toast.makeText(this, "HelloWorld", Toast.LENGTH_SHORT).show();
}


三種寫法效果相當,實際上任何方法都能這樣綁到控件上,可以自己試試。
雙向綁定的意思是:介面上的輸入會同步回物件,物件的變動也會反映到介面。要啟用它,資料類別必須繼承 BaseObservable,並在 getter 上加 @Bindable:
import android.databinding.BaseObservable;
import android.databinding.Bindable;
public class Student extends BaseObservable {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}
@Bindable
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
notifyPropertyChanged(BR.age);
}
}
關鍵是 setter 中的 notifyPropertyChanged(BR.xxx)——少了這行,介面不會自動更新。
布局檔方面,把 EditText 的 text 改成 @{student.name} 的形式,前面多一個等號:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@={student.name}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@{student.name}" />
也就是 @{student.name} 變成 @={student.name}。MainActivity 不需要任何改動。

在 EditText 裡打字,下方 TextView 即時跟著變,這就是雙向綁定的效果。
最基礎的部分到此結束。DataBinding 還有許多進階特性,這裡就不深入了。寫這篇的目的只是記錄自己的學習過程。
Android Activity 完整指南:建立、生命週期與啟動模式
Activity 是 Android 四大元件之一,負責使用者介面。建立與跳轉繼承 Activity 並在 AndroidManifest.xml 宣告,可用顯式 Int
Android RecyclerView 與 CardView 快速上手
新 UI 元件 Android 5.0 之後,RecyclerView 與 CardView 成為常見的列表與卡片元件。RecyclerView 可視為更靈活的 List
ViewPager 多頁滑動實作:support 套件配置與 FragmentPagerAdapter 完整示例
1、先加入 android support 套件 ViewPager、PagerTabStrip 這些類別都只有在 android support 套件中才提供,因此第一
Android 傳感器入門:分類、坐標系、核心 API 與開發避坑指南
接下來我們進入 Android 的一些進階主題:傳感器、GPS、NFC、語音與人臉識別等。本文先從傳感器開始,把分類、坐標系、框架 API 與實務注意事項一次講清楚。