Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自製小工具:一鍵把布局檔轉成 findViewById 程式碼

編輯:關於 Android 程式設計

公司網路實在堪憂,插件市場時好時壞;而每次都要手打一堆 findViewById 也確實枯燥。於是我就自己寫了個小工具:讀入布局檔,自動產生對應的宣告與綁定程式碼,並直接複製到剪貼簿。

輸入的布局檔

以下是一個典型的「兒童保護」設定頁,包含標題列、四個輸入欄位與一顆確認鈕:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/home_backgroud"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <RelativeLayout
            android:id="@+id/headerLine"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#3d61f3"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/title"
                style="@style/header"
                android:text="兒童保護" />

            <ImageView
                android:id="@+id/setchild_menu_btn"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_back" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:orientation="vertical">

        <!-- 開始時間 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:text="開始時間"
                android:gravity="center_vertical" />
            <EditText
                android:id="@+id/et_childstarttime"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:singleLine="true" />
        </LinearLayout>

        <!-- 結束時間 -->
        <LinearLayout ... >
            <EditText
                android:id="@+id/et_childendtime"
                android:singleLine="true" />
        </LinearLayout>

        <!-- 性別 -->
        <LinearLayout ... >
            <EditText
                android:id="@+id/et_childsex"
                android:hint="男/女"
                android:singleLine="true" />
        </LinearLayout>

        <!-- 週期 -->
        <LinearLayout ... >
            <EditText
                android:id="@+id/et_childperiod"
                android:singleLine="true" />
        </LinearLayout>

        <LinearLayout ... >
            <Button
                android:id="@+id/setchild_ok"
                android:text="確認" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

工具吐出的結果

工具掃過所有帶 id 的節點,依型別產生成員變數宣告,再補上一個 bindview() 方法把對應關係一次綁好:

private RelativeLayout headerLine;
private TextView title;
private ImageView setchild_menu_btn;
private EditText et_childstarttime;
private EditText et_childendtime;
private EditText et_childsex;
private EditText et_childperiod;
private Button setchild_ok;

private void bindview() {
    headerLine        = (RelativeLayout) findViewById(R.id.headerLine);
    title             = (TextView) findViewById(R.id.title);
    setchild_menu_btn = (ImageView) findViewById(R.id.setchild_menu_btn);
    et_childstarttime = (EditText) findViewById(R.id.et_childstarttime);
    et_childendtime   = (EditText) findViewById(R.id.et_childendtime);
    et_childsex       = (EditText) findViewById(R.id.et_childsex);
    et_childperiod    = (EditText) findViewById(R.id.et_childperiod);
    setchild_ok       = (Button) findViewById(R.id.setchild_ok);
}

實際上,如今這類需求已有 ButterKnife、Android KTX 擴充函式等成熟方案可用,不必自己造輪子。但從這個小練習能釐清一個觀念:findViewById 本質上就是依 id 在檢視樹中查找節點,型別由布局檔決定。理解這一層之後,再回頭看那些「自動化」的庫,就會發現它們做的也不過是把同樣的查找與轉型,提前到編譯期或透過產生程式碼完成而已。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved