最近項(xiàng)目需求,需要一個(gè)好看一點(diǎn)的提示框,網(wǎng)上搜羅一番,發(fā)現(xiàn)sweetDialog 人氣很高,但是到了實(shí)際使用的時(shí)候,發(fā)現(xiàn)有些問(wèn)題不太好解決,而且已經(jīng)三年沒(méi)有更新了,而其他的庫(kù),更是達(dá)不到要求。所以索性自己寫(xiě)了一個(gè)smile-dialog,這里放出來(lái),希望對(duì)大家有所幫助。
目前實(shí)現(xiàn)比較簡(jiǎn)單,只是一個(gè)簡(jiǎn)單的提示框(warning、success、error 三種狀態(tài)),以后持續(xù)添加新的功能。
話不多說(shuō),先上效果圖:

使用方法
1.添加Gradle依賴
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.rosefinches:smile-dialog:1.0.1'
}
2.主要代碼實(shí)現(xiàn)
SmileDialog dialog = new SmileDialogBuilder(MainActivity.this, SmileDialogType.ERROR)
.setTitleText(R.string.app_name)
.setTitleText("222")
.setContentText(null)
.setConformBgResColor(R.color.colorPrimary)
.setTitleTextResColor(R.color.colorAccent)
.setContentTextColor(Color.RED)
.setConformTextColor(Color.GREEN)
.setCancelTextColor(Color.GREEN)
// .setCanceledOnTouchOutside(false)
// .setCancelable(false)
// .hideTitle(true)
.setConformButton("sss")
.setWindowAnimations(R.style.dialog_style)
.build();
dialog.show();
主要實(shí)現(xiàn)
Dialog實(shí)現(xiàn)
Dialog實(shí)現(xiàn)比較簡(jiǎn)單,繼承DialogFragment,繪制布局;
Icon動(dòng)畫(huà)實(shí)現(xiàn)
icon 由Vector繪制,動(dòng)畫(huà)由animated-vector + objectAnimator 實(shí)現(xiàn),關(guān)于vector 動(dòng)畫(huà)效果,有興趣的童鞋可以去網(wǎng)上搜羅一波,有不少介紹,或者,直接偶的代碼 https://github.com/rosefinches/smile-dialog,這里就不多說(shuō)了。
Api 說(shuō)明
| 方法 | 說(shuō)明 |
|---|---|
| setTitleText() | 設(shè)置標(biāo)題文字 |
| setTitleTextColor() / setTitleTextResColor() | 設(shè)置標(biāo)題字體顏色 |
| setContentText() | 設(shè)置內(nèi)容文字 |
| setContentTextColor() / setContentTextResColor() | 設(shè)置內(nèi)容字體顏色 |
| setConformButton() | 設(shè)置確認(rèn)按鈕文字 |
| setOnConformClickListener(listener) | 設(shè)置確認(rèn)監(jiān)聽(tīng)事件 |
| setCancelButton() | 設(shè)置取消按鈕文字(默認(rèn)隱藏,有文字,則顯示) |
| setOnCancelClickListener(listener) | 設(shè)置取消監(jiān)聽(tīng)事件 |
| setConformTextColor() / setConformTextResColor() | 設(shè)置確認(rèn)按鈕文字顏色 |
| setCancelTextColor() / setCancelTextResColor() | 設(shè)置取消按鈕文字顏色 |
| setConformBgColor() / setConformBgResColor() | 設(shè)置確認(rèn)按鈕背景顏色 |
| setCancelBgColor() / setCancelBgResColor() | 設(shè)置取消按鈕背景顏色 |
| hideTitle() | 隱藏標(biāo)題 |
| hideIcon() | 隱藏Icon |
| setCancelable() | 是否可以取消 |
| setCanceledOnTouchOutside() | 點(diǎn)擊區(qū)域外是否消失 |
| setWindowAnimations() | 設(shè)置動(dòng)畫(huà)style |
說(shuō)明:
- 所有設(shè)置文字的方法,參數(shù)都有兩種,分別是(@NonNull CharSequence text)和(@StringRes int res);
- 所有設(shè)置顏色的方法,參數(shù)有三種,分別是(@ColorInt int color)、(ColorStateList color) 和(@ColorRes int color) ,前兩者是setXXXColor(),后者為setResColor();
- 設(shè)置按鈕文字時(shí),如果只有一個(gè)參數(shù),則為按鈕文字,如有第二個(gè)參數(shù),則表示給按鈕添加點(diǎn)擊事件
- 所有方法都是有參數(shù)的,參數(shù)請(qǐng)參考實(shí)際代碼
寫(xiě)在最后
這是一個(gè)簡(jiǎn)單的AlertDialog實(shí)現(xiàn),本猿希望長(zhǎng)期更新,各位有什么問(wèn)題或者有什么建議,還請(qǐng)大家反饋一下,感謝大家支持!
項(xiàng)目地址:https://github.com/rosefinches/smile-dialog