編輯:關於 Android 程式設計
最近我獨立開發的專案《全醫會》已經進入內測階段,很快會上架各大應用市場。之前做的幾個專案因為種種原因最終沒能上架,一直是我的遺憾,所以這次心情格外好。
目前在進行一個新專案,是專為律師與客戶設計的 App,其中有一個自定義對話框的需求。這個知識點本身並不難,效果就是下圖這樣:

可是悠閒地寫完之後我發現了一件要命的事:自定義對話框裡嵌套的 EditText 根本無法取得焦點,也彈不出軟鍵盤。之前寫過的幾個自定義對話框裡都沒有 EditText,所以這個坑一直沒被踩到。以下是我先前寫過的一個版本:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="260dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="@drawable/round_white_corner"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp" >
<EditText
android:id="@+id/et_obj"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="30dp"
android:background="@drawable/round_textview_corner"
android:gravity="center_vertical"
android:hint="輸入標的"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="14sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="12dp"
android:background="@drawable/round_textview_corner"
android:gravity="center_vertical"
android:hint="聯繫人姓名"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="14sp" />
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="12dp"
android:background="@drawable/round_textview_corner"
android:gravity="center_vertical"
android:hint="聯繫人電話"
android:inputType="phone"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_go"
android:layout_width="110dp"
android:layout_height="35dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="25dp"
android:background="@drawable/round_blue_corner"
android:gravity="center"
android:text="確認委託"
android:textColor="@color/white"
android:textSize="16sp" >
</TextView>
</LinearLayout>
<ImageView
android:id="@+id/iv_close"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_marginLeft="-50dp"
android:layout_marginTop="5dp"
android:src="@drawable/law_dialog_close" />
</LinearLayout>
</LinearLayout>

// 彈出自定義 dialog LayoutInflater inflater = LayoutInflater.from(OnlineActivity.this); View view = inflater.inflate(R.layout.hl_law_dialog, null); // 建立對話框(重點:使用 Dialog 而非 AlertDialog) final Dialog dialog = new Dialog(OnlineActivity.this); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.show(); // 設定寬度為螢幕寬度 WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); lp.width = display.getWidth(); dialog.getWindow().setAttributes(lp); dialog.getWindow().setContentView(view); final EditText et_obj = (EditText) view.findViewById(R.id.et_obj); // 標的 final EditText et_name = (EditText) view.findViewById(R.id.et_name); // 姓名 final EditText et_phone = (EditText) view.findViewById(R.id.et_phone); // 電話 TextView tv_go = (TextView) view.findViewById(R.id.tv_go); // 確認委託 final ImageView iv_close = (ImageView) view.findViewById(R.id.iv_close);
之前我的寫法是這樣:
// 對話框 final Dialog dialog = new AlertDialog.Builder(OnlineActivity.this).create(); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.show();
關鍵差異就在於——我把 AlertDialog 換成了 Dialog。AlertDialog 內部對焦點的處理方式會與自訂版面中的 EditText 衝突,導致無法取得焦點、軟鍵盤也就不會彈出。改成直接使用 Dialog,問題立刻解決。
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

畫面上會多出一條醜陋的標題欄。
WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); lp.width = display.getWidth(); dialog.getWindow().setAttributes(lp);

對話框會被壓得又窄又難看。
小結一下這個坑:自定義 Dialog 裡要放可輸入的控制項時,請直接用 Dialog 而不是 AlertDialog.Builder,同時別忘了 requestWindowFeature(Window.FEATURE_NO_TITLE) 與手動設定寬度這兩步。
Android 藍牙對戰五子棋項目實戰(含人機對戰功能)
這是上周花了一週時間完成的課程設計項目,主要實現了以下功能: 基本功能 (1)該 App 能透過藍牙自動搜索周圍其他安裝了本應用的手機,用戶可選擇其中某一台發起對戰邀請,
Android 手勢密碼元件詳解:開源庫用法與底層實作
簡介 最近專案需要用到手勢解鎖,翻了一些 demo 都覺得要改的地方太多,索然自己包了一層。這個開源庫主要提供這些能力: 支援手勢繪製與密碼保存,解鎖時自動比對並回報結
LinearLayout 與 RelativeLayout 組合完成精細版面配置
padding 與 margin 的差異padding 是控制項內容與控制項邊緣之間的距離;margin 則是控制項邊緣與父容器之間的距離。兩者都用來處理間距,但參照對象
Android模擬器接收UDP數據包問題排查
在Android開發與測試中,UDP常用於輕量級資料傳輸,例如裝置發現、即時狀態推送與區域網路通信。但在Android模擬器上接收UDP封包時,常會遇到收不到資料的問題。