編輯:關於 Android 程式設計
在 Android 專案裡,彈窗幾乎是每個 App 都躲不開的需求:提示確認、加載等待、列表選擇、登錄輸入框等等。如果每次都要重新寫一遍 Dialog 的樣式與樣板代碼,不僅效率低,風格也很難統一。
本文要介紹的是一個名為 jjdxm_dialogui 的開源庫,它在 AlertDialog 與 Dialog 兩個基礎類之上做了二次封裝,把常見的十幾種彈窗樣式抽成了靜態方法,調用時一行代碼即可彈出,同時保留了自定義視圖的能力。
項目地址:https://www.github.com/jjdxmashl/jjdxm_dialogui
1. 採用鏈式調用風格,代碼簡潔直觀,一個 show() 收尾即可。
2. 所有彈窗樣式統一收斂在 DialogUIUtils 類中,方法一目了然,便於查閱。
3. 支持自定義彈窗內部的字體與配色樣式。
4. 類似加載框的簡易提示樣式提供白色與灰色兩套主題,可自由切換以適配深色背景。












項目已發佈到 Maven 中央倉庫,demo apk 也可直接下載體驗。
Maven 方式:
com.dou361.dialogui jjdxm-dialogui x.x.x
Gradle 方式:
compile 'com.dou361.dialogui:jjdxm-dialogui:x.x.x'
歷史版本可寫死為 1.0.1 或 1.0.0。運行環境要求最低 Java 9 與 Android 2.3。若遇到打包引用衝突,可通過 exclude 排除重複的依賴項來解決。
根據你的混淆器配置情況,可能需要在 proguard 文件中加入以下規則,避免反射相關的類被裁剪:
-keep class com.dou361.** { *; }
如果你需要使用內建的 toast 類提示,該類採用了單例模式,多次調用只會保留一個實例,因此必須先完成初始化,否則會拋出空指針異常。使用 Application 的 Context 調用一次即可:
DialogUIUtils.init(appContext);
如果項目裡完全不用 toast 相關方法,這一步可以跳過,直接調用對應的彈窗方法就行。
三個重載方法分別對應「默認居中」「指定位置」「完全自定義」三種場景:
// 默認居中、可取消、可點擊外部 public static BuildBean showCustomAlert(Context context, View contentView) // 指定顯示位置,例如 Gravity.center public static BuildBean showCustomAlert(Context context, View contentView, int gravity) // cancleable:false 表示不可取消;outsideTouchable:false 表示點擊空白處不消失 public static BuildBean showCustomAlert(Context context, View contentView, int gravity, boolean cancleable, boolean outsideTouchable)
使用方式很直觀,先 inflate 一個自定義佈局,再傳進去:
View rootView = View.inflate(activity, R.layout.custom_dialog_layout, null); DialogUIUtils.showCustomAlert(this, rootView).show();
// 默認白色背景 public static BuildBean showToastTie(Context context, CharSequence msg) // isWhiteBg 為 false 時使用灰色背景 public static BuildBean showToastTie(Context context, CharSequence msg, boolean isWhiteBg) public static BuildBean showToastTie(Context context, CharSequence msg, boolean cancleable, boolean outsideTouchable, boolean isWhiteBg)
DialogUIUtils.showToastTie(this, "加載中...").show();
加載框分為橫向與豎向、普通風格與 Material Design 風格,共四組方法,參數結構完全一致:
DialogUIUtils.showLoadingHorizontal(this, "加載中...").show(); DialogUIUtils.showMdLoadingHorizontal(this, "加載中...").show(); DialogUIUtils.showLoadingVertical(this, "加載中...").show(); DialogUIUtils.showMdLoadingVertical(this, "加載中...").show();
每組都提供了帶 isWhiteBg 以及帶 cancleable、outsideTouchable 的重載版本。
DialogUIUtils.showMdAlert(activity, "標題", "文本內容", new DialogUIListener() {
@Override
public void onPositive() { }
@Override
public void onNegative() { }
}).show();
標題參數傳 null 時即為無標題樣式。
String[] words = new String[]{"1", "2", "3"};
boolean[] choseDefault = new boolean[]{false, false, false};
DialogUIUtils.showMdMultiChoose(activity, "標題", words, choseDefault, new DialogUIListener() {
@Override
public void onPositive() { }
@Override
public void onNegative() { }
}).show();
DialogUIUtils.showSingleChoose(activity, "單選", 0, words, new DialogUIItemListener() {
@Override
public void onItemClick(CharSequence text, int position) {
showToast(text + "--" + position);
}
}).show();
單選框的 defaultChosen 用於指定默認選中項的下標。
提供居中、橫向、豎向三種排布:
DialogUIUtils.showAlert(activity, "標題", "文本內容", new DialogUIListener() {
@Override
public void onPositive() { }
@Override
public void onNegative() { }
}).show();
DialogUIUtils.showAlertHorizontal(activity, "標題", "文本內容", listener);
DialogUIUtils.showAlertVertical(activity, "標題", "文本內容", listener);
居中的列表樣式:
Liststrings = new ArrayList<>(); strings.add("1"); strings.add("2"); strings.add("3"); DialogUIUtils.showCenterSheet(activity, strings, new DialogUIItemListener() { @Override public void onItemClick(CharSequence text, int position) { } @Override public void onBottomBtnClick() { } }).show();
帶取消按鈕的底部彈出列表,取消按鈕文案可自定義:
DialogUIUtils.showBottomSheetAndCancel(activity, strings, "取消", new DialogUIItemListener() {
@Override
public void onItemClick(CharSequence text, int position) { }
@Override
public void onBottomBtnClick() { }
}).show();
普通底部彈出列表需要傳入 BottomSheetBean 集合,第一個參數可配置 item 類型:
Listdatass = new ArrayList<>(); datass.add(new BottomSheetBean(0, "1")); datass.add(new BottomSheetBean(0, "2")); datass.add(new BottomSheetBean(0, "3")); DialogUIUtils.showBottomSheet(this, datass, new DialogUIItemListener() { @Override public void onItemClick(CharSequence text, int position) { } }).show();
MD 風格的底部列表分豎向與橫向兩種,橫向版本還可以通過 columnsNum 指定列數:
DialogUIUtils.showMdBottomSheetVertical(this, datass, listener); DialogUIUtils.showMdBottomSheetHorizontal(this, datass, listener);
支持雙輸入框,常用於登錄、修改密碼等場景:
DialogUIUtils.showAlertInput(activity, "登錄", "請輸入用戶名", "請輸入密碼", "登錄", "取消", new DialogUIListener() {
@Override
public void onPositive() { }
@Override
public void onNegative() { }
@Override
public void onGetInput(CharSequence input1, CharSequence input2) {
// 在這裡拿到兩個輸入框的內容
}
}).show();
這套封包的價值在於把「重複的樣板」變成「一次調用」,同時用 BuildBean 保留了鏈式配置的擴展空間。對於中小型專案,它能明顯減少 Dialog 相關代碼量;對於設計風格特殊的專案,也可以直接走 showCustomAlert 完全接管視圖。
項目地址:https://www.github.com/jjdxmashl/jjdxm_dialogui
Android 跨應用綁定 Service 並通信(AIDL 實戰)
上一篇記錄了跨應用「啟動」Service,這篇來談跨應用「綁定」Service。差別在於:startService 是單向的,啟動方丟下 Intent 就走;而 bind
Android 項目目錄結構解析
了解 Android 工程的目錄結構,是入門開發的基礎。不同 SDK 版本生成的目錄可能略有差異,但核心組成基本一致。核心目錄src 目錄存放 Java 源代碼;gen
Android Socket 通信入門:基於 TCP 的服務端與客戶端實作
Socket 是基於 TCP 的通信端點。伺服器端建立 ServerSocket 並綁定 port,呼叫 accept 等待客戶端;客戶端則建立 Socket 連線到伺服
Cordova InAppBrowser 插件改造:讓 WebView 的 input type=file 支援選擇文件
改造後支援的能力 1. 幾乎支援所有 Android 版本,從 2.x 到 7.0 Android 4.4、4.4.1 與 4.4.2 無法支援,因為當時 Google