自定義佈局作為 Dialog,點擊按鈕獲取 EditText 內容時閃退,findViewById 返回 null。
直接調用 findViewById 查找的是 Activity/Dialog 的視圖樹,而非自定義佈局視圖樹。
// 1. 加載佈局得到根視圖
View v = LayoutInflater.from(context).inflate(R.layout.aaa, null);
// 2. Dialog 設置內容視圖
dialog.setContentView(v);
// 或 PopupWindow 構造時傳入 v
// 3. 在根視圖 v 上查找控件
EditText et = v.findViewById(R.id.my_edittext);
String text = et.getText().toString();
findViewById 作用域限定在調用者的視圖層級inflate 得到 View 實例View 實例上進行作者開源項目 BasePopup 封裝了各類 Popup 交互,可直接引用。