開源專案material-dialogs使用
匯入
compile 'com.afollestad.material-dialogs:core:0.9.1.0'
compile 'com.afollestad.material-dialogs:commons:0.9.1.0'
具體的使用
- Basic Dialog
簡單的dialog
new MaterialDialog.Builder(MainActivity.this)
.title("basic dialog")
.content ("一個簡單的dialog,高度會隨著內容改變,同時還可以巢狀RecyleView")
.iconRes(R.drawable.icon)
.positiveText("同意")
.negativeText("不同意")
.neutralText("更多資訊")
.widgetColor (Color.BLUE)//不再提醒的checkbox 顏色
//CheckBox
.checkBoxPrompt("不再提醒", false, new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
Toast.makeText (MainActivity.this, "不再提醒", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "會再次提醒", Toast.LENGTH_LONG).show();
}
}
})
//巢狀recycleview,這個的點選事件可以先獲取此Recycleview物件然後自己處理
.adapter(new RecycleviewAdapter(getData(), MainActivity.this), new LinearLayoutManager(MainActivity.this))
.itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
dataChoose = "下標:" + position + " and 資料:" + mData.get(position);
}
})
//點選事件新增 方式1
.onAny(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
if (which == DialogAction.NEUTRAL) {
Toast.makeText(MainActivity.this, "更多資訊", Toast.LENGTH_LONG).show();
} else if (which == DialogAction.POSITIVE) {
Toast.makeText(MainActivity.this, "同意" + dataChoose, Toast.LENGTH_LONG).show();
} else if (which == DialogAction.NEGATIVE) {
Toast.makeText(MainActivity.this, "不同意", Toast.LENGTH_LONG).show();
}
}
})
.show();
相應的效果:
簡單的diaolog,同意和不同意的字型顏色是預設是R.color.colorAccent
文字變多的時候會自動拉長高度
巢狀一個recycleview,這個的點選事件可以先獲取此Recycleview物件然後自己處理
.adapter(new RecycleviewAdapter(getData(), MainActivity.this), new LinearLayoutManager(MainActivity.this))
//
RecycleView rc= dialog.getRecyclerView();
這裡新增一個更多資訊的按鈕
.neutralText("更多資訊")
點選事件
- 方式一
.onAny(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
if (which == DialogAction.NEUTRAL) {
Toast.makeText(MainActivity.this, "更多資訊", Toast.LENGTH_LONG).show();
} else if (which == DialogAction.POSITIVE) {
Toast.makeText(MainActivity.this, "同意" + dataChoose, Toast.LENGTH_LONG).show();
} else if (which == DialogAction.NEGATIVE) {
Toast.makeText(MainActivity.this, "不同意", Toast.LENGTH_LONG).show();
}
}
})
public Builder onAny(@NonNull SingleButtonCallback callback) {
this.onAnyCallback = callback;
return this;
}
public interface SingleButtonCallback {
void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which);
}
public enum DialogAction {
POSITIVE,
NEUTRAL,
NEGATIVE
}
就是傳一個SingleButtonCallback 介面,用DialogAction 來區分現在是那個Action 請求,然後對應的處理
- 方式二
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Toast.makeText(MainActivity.this,"同意",Toast.LENGTH_LONG).show();
}
})
//.onNegative()
//.onNeutral()
單個按鈕新增監聽和onAny 其實一樣
- 方式三
這個方法已經過時了
.callback(new MaterialDialog.ButtonCallback() {//新增按鈕點選監聽
@Override
public void onPositive(MaterialDialog dialog) {
super.onPositive(dialog);
Toast.makeText(MainActivity.this,"同意",Toast.LENGTH_LONG).show();
}
@Override
public void onNegative(MaterialDialog dialog) {
super.onNegative(dialog);
Toast.makeText(MainActivity.this,"不同意",Toast.LENGTH_LONG).show();
}
@Override
public void onNeutral(MaterialDialog dialog) {
super.onNeutral(dialog);
Toast.makeText(MainActivity.this,"更多資訊",Toast.LENGTH_LONG).show();
}
})
新增checkbox
.checkBoxPrompt("不再提醒", false, new CompoundButton.OnCheckedChangeListener() {//check事件
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
Toast.makeText(MainActivity.this, "不再提醒", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "會再次提醒", Toast.LENGTH_LONG).show();
}
}
})
- 顏色修改
.widgetColor(Color.BLUE)
其他屬性介紹
.btnSelector(R.color.colorPrimary)//按鈕的背景顏色
//分開設定2個按鈕的背景顏色
// .btnSelector(R.color.colorPrimary, DialogAction.NEGATIVE)
// .btnSelector(R.color.colorPrimaryDark, DialogAction.POSITIVE)
// .btnSelector(R.color.colorPrimary,DialogAction.NEUTRAL)
// .backgroundColor(Color.parseColor("#FF9988"))//dialog的背景顏色
// .contentColor(Color.WHITE)//內容字型的顏色
- List Dialogs
列表彈框,.item ()來新增類別內容,也可以是
.items(new String[]{"AAAA","BBBBB","CCCCC","DDDDDDDD","EEEEE","FFFFFF","GGGGGG","HHHHHHH"})
new MaterialDialog.Builder(MainActivity.this)
.title("List Dialog")
.iconRes(R.drawable.ic_logo)
.content("List Dialog,顯示陣列資訊,高度會隨著內容擴大")
.items(R.array.item)
//.listSelector(R.color.green)//列表的背景顏色
.autoDismiss(false)//不自動消失
.show();
- 列表點選事件
.itemsCallback(new MaterialDialog.ListCallback() {//選中監聽,同時dialog消失
@Override
public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
dataChoose += "下標:" + position + " and 資料:" + text;
Toast.makeText(MainActivity.this, dataChoose, Toast.LENGTH_LONG).show();
}
})
- 新增單選
//單選
.itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {//0 表示第一個選中 -1 不選
@Override
public boolean onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
dataChoose="此時選中的下標"+which;
Toast.makeText(MainActivity.this,dataChoose,Toast.LENGTH_LONG).show();
return true;
}
})
- Multi Choice List Dialogs
多選dialog
new MaterialDialog.Builder(MainActivity.this)
.title("Multi Choice List Dialogs")
.iconRes(R.drawable.ic_logo)
.content("Multi Choice List Dialogs,顯示陣列資訊,高度會隨著內容擴大.可以多選")
.items(R.array.item)
.positiveText("確定")
.widgetColor(Color.RED)//改變checkbox的顏色
//多選框新增
.itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() {
@Override
public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
return true;//false 的時候沒有選中樣式
}
})
//點選確定後獲取選中的下標陣列
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, "選中" + dialog.getSelectedIndices().length + "個", Toast.LENGTH_LONG).show();
}
})
.show();
這裡有一個itemsCallbackMultiChoice()他的返回結果決定了checkbox 是否有選中和取消的效果。
- Custom Views
可以引入外部view的dialog
new MaterialDialog.Builder(MainActivity.this)
.customView(R.layout.custome_view,false)
.show();
效果:
customView( int layoutRes, boolean wrapInScrollView)
當我們將wrapInScrollView設定為true的時候就表示需要一個padding
假設佈局中有個按鈕我們要點選這個按鈕關閉dialog可以這樣操作
final MaterialDialog dialog = new MaterialDialog.Builder(MainActivity.this)
.customView(R.layout.custome_view, false)
.show();
View customeView = dialog.getCustomView();
Button button = (Button) customeView.findViewById(R.id.btn_closeCustome);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
- Input Dialogs
含有輸入框的彈框
new MaterialDialog.Builder(MainActivity.this)
.title("輸入窗")
.iconRes(R.drawable.ic_logo)
.content("包含輸入框的diaolog")
// .widgetColor(Color.BLUE)//輸入框游標的顏色
.inputType(InputType.TYPE_CLASS_PHONE)//可以輸入的型別-電話號碼
//前2個一個是hint一個是預輸入的文字
.input(R.string.input_hint, R.string.input_prefill, new MaterialDialog.InputCallback() {
@Override
public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
Log.i("yqy", "輸入的是:" + input);
}
})
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
if (dialog.getInputEditText().length() <=10) {
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
}else {
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
}
}
})
.show();
限制條件
輸入型別是 .inputType(InputType.TYPE_CLASS_PHONE)//可以輸入的型別-電話號碼,限制輸入長度是inputRange(11, 41, R.color.colorAccent)//限制輸入的長度,只有在(11,41)長度內ok按鈕才是可用的。限制長度還可以是在onInput()方法裡處理。
if (dialog.getInputEditText().length() < 10) {
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
}else{
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
}
while (dialogPro.getCurrentProgress()!=dialogPro.getMaxProgress()){
if (dialogPro.isCancelled()) break;
try {
Thread.sleep(50);//模擬載入時間
} catch (InterruptedException e) {
break;
}
dialogPro.incrementProgress(1);
}
dialogPro.setContent("載入完成");
- Progress Dialog
進度條彈框應該是用的比較多的了,但是這個的我感覺用起來不好,一般的點選載入進度
設定成水平的進度條
.progress(false, 100, true)
.progressNumberFormat("%1d/%2d")
//progress(boolean indeterminate, int max, boolean showMinMax)
// false 的話是水平進度條,true是等待圓環 最大值 是否顯示數值,false的時候是不可以用progress這類的屬性的灰報錯:Cannot use setProgress() on this dialog.
沒有一個載入進度的過程
- ColorDialog
有關顏色選擇的相關彈框,選擇的時候實現選擇的回掉介面:ColorChooserDialog.ColorCallback
@Override
public void onColorSelection(@NonNull ColorChooserDialog dialog, @ColorInt int selectedColor) {
//此時選擇的顏色
}
new ColorChooserDialog.Builder(MainActivity.this,R.string.app_name)
.titleSub(R.string.input_hint) // title of dialog when viewing shades of a color
.accentMode(false) // when true, will display accent palette instead of primary palette
.doneButton(R.string.md_done_label) // changes label of the done button
.cancelButton(R.string.md_cancel_label) // changes label of the cancel button
.backButton(R.string.md_back_label) // changes label of the back button
.preselect(Color.RED) // 開始的時候的預設顏色
.dynamicButtonColor(true) // defaults to true, false will disable changing action buttons' color to currently selected color
.show();
可以自己定義篩選的顏色
int[] primary = new int[] {
Color.parseColor("#F44336")
};
int[][] secondary = new int[][] {
new int[] { Color.parseColor("#EF5350"), Color.parseColor("#F44336"), Color.parseColor("#E53935") }
};
new ColorChooserDialog.Builder(MainActivity.this, R.string.app_name)
// .titleSub(R.string.app_name)
.customColors(primary, secondary)
.doneButton(R.string.done)
.cancelButton(R.string.cancel)
.titleSub(R.string.done)//設定二級選擇的標題
// .presetsButton(R.string.input_hint)//從RRGB切換到CUstome的文字提示
.show();
new ColorChooserDialog.Builder(MainActivity.this, R.string.app_name)
.allowUserColorInput(false)
.customButton(R.string.md_custom_label)
.presetsButton(R.string.md_presets_label)
.show();
- File Selector Dialogs
檔案彈框
new FileChooserDialog.Builder(MainActivity.this)
.initialPath("/sdcard/Download") // changes initial path, defaults to external storage directory
.mimeType("image/*") // Optional MIME type filter
.extensionsFilter(".png", ".jpg") // Optional extension filter, will override mimeType()
.tag("optional-identifier")
.goUpLabel("Up") // custom go up label, default label is "..."
.show();
實現介面
@Override
public void onFileSelection(@NonNull FileChooserDialog dialog, @NonNull File file) {
//選擇檔案
}