編輯:關於 Android 開發
註冊頁幾乎是每個 App 的標配。這裡用 TableLayout 配合 TableRow,把「標籤 + 輸入框」按行對齊排列,既能保證整齊,又能自動適配不同屏幕寬度。
頁面效果如下:

關鍵在於根節點的 android:stretchColumns="1",它讓第二列(輸入框)自動拉伸填滿剩餘寬度,這樣不管文字長短,右邊界都能對齊。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
android:background="@drawable/bluesky">
<!-- 賬號 -->
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="賬號:"
android:textSize="16sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="手機號"
android:selectAllOnFocus="true" />
</TableRow>
<!-- 密碼 -->
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密碼:"
android:textSize="16sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword" />
</TableRow>
<!-- 生日 -->
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="生日:"
android:textSize="16sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date" />
</TableRow>
<!-- 住址 -->
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="住址:"
android:textSize="16sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPostalAddress" />
</TableRow>
<!-- 電子郵箱 -->
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="電子郵箱:"
android:textSize="16sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
</TableRow>
<!-- 註冊 -->
<TableRow>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="註冊" />
</TableRow>
</TableLayout>
這個例子最值得借鑑的地方,是用 android:inputType 為不同字段匹配合適的鍵盤與輸入約束,這一步做對了,用戶的輸入體驗會好很多:
numberPassword:只彈出數字鍵盤,同時以密文顯示,適合純數字密碼;date:提示系統這是一個日期字段,部分鍵盤會提供日期相關的輸入輔助;textPostalAddress:地址輸入專用,鍵盤行為更貼合填寫地址的場景;textEmailAddress:鍵盤會自帶 @ 和 . 等符號,輸入郵箱更順手。其他常用的還有 phone(電話號碼)、number(純數字)、textPassword(通用密文)、textMultiLine(多行輸入)等。
密碼字段建議再補一個 android:maxLength,從源頭限制超長輸入,避免後端截取帶來的不一致。
如果這個表單要長期維護,更推薦改用 LinearLayout 或 ConstraintLayout。TableLayout 在列數固定、需要嚴格對齊的場景下很合適,但當字段較多、還需要錯誤提示文案和異步校驗狀態時,其他布局配合 TextInputLayout 會更好維護。
Eclipse 字體設定與 Genymotion 模擬器
改善 Eclipse 的可讀性 長時間撰寫程式時,字型選擇會直接影響閱讀舒適度。若想讓 Eclipse 看起來更順眼,可以先下載喜歡的字型,安裝到系統字型資料夾,再重新啟
自訂環形(圓形)進度條的實作教學
本文參考並借鑑自 https://blog.csdn.net/jdsjlzx/article/details/42497135 先看效果圖: 自訂控制項:Attenda
Android 內存洩漏的幾種典型場景總結
Java 是帶垃圾回收的語言,它的優勢在於開發者無需手動管理內存分配,既降低了因內存錯誤導致進程崩潰的風險,也避免了未釋放的內存把堆空間撐爆。但這並不意味著內存問題從此消
Android 自訂樣式與主題入門:style 加在 View,theme 加在 Application
Android 自訂樣式與主題入門:style 加在 View,theme 加在 Application 這篇做一個簡單練習,釐清 style 與 theme 最基本的差