1. 程式人生 > >學習筆記Android彈框material-dialogs

學習筆記Android彈框material-dialogs

新增依賴

core版:可以建立basic, list, single/multi choice, progress, input等彈框。

implementation 'com.afollestad.material-dialogs:core:0.9.6.0'

common版:可以建立更多拓展的彈框包括ColorChooserDialog, FolderChooserDialog, the Material Preference classes, and MaterialSimpleListAdapter/MaterialSimpleListItem等等。

implementation 'com.afollestad.material-dialogs:commons:0.9.6.0'

1、基礎彈框

這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("標題")
                .content("內容")
                .positiveText("確認")
                .negativeText("取消")
                .show();

2、取消彈框

得這樣new

MaterialDialog dialog = new MaterialDialog.Builder(this)
        .title(R.string.title)
        .content(R.string.content)
        .positiveText(R.string.agree)
        .show();

這樣取消

dialog.dismiss();

3、新增圖示

這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("標題")
                .content("內容")
                .positiveText("確認")
                .negativeText("取消")
                .icon(getResources().getDrawable(R.drawable.ic_account_circle_black_24dp))
                .show();

可以用maxIconSize(int size)限制最大size

4、自適應按鈕

如果按鈕太長,它會自動縱向排列
這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("標題")
                .content("內容")
                .positiveText("確認確認確認確認確認確認確認確認確認確認")
                .negativeText("取消取消取消取消取消取消取消取消取消取消")
                .icon(getResources().getDrawable(R.drawable.ic_account_circle_black_24dp))
                .show();

5、中立按鈕

除了確認類和取消類按鈕還有中立按鈕,它會在最左邊
這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("標題")
                .content("內容")
                .positiveText("確認")
                .negativeText("取消")
                .neutralText("更多")
                .show();

6、回撥

        new MaterialDialog.Builder(this)
                .title("標題")
                .content("內容")
                .positiveText("確認")
                .negativeText("取消")
                .neutralText("更多")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog dialog, DialogAction which) {
                        // TODO
                    }
                })
                .onNeutral(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog dialog, DialogAction which) {
                        // TODO
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog dialog, DialogAction which) {
                        // TODO
                    }
                })
                .show();
            

如果你想監聽三種動作按鈕,只用一個onAny就行了,which會告訴你點選了什麼按鈕,根據需要去判斷

        new MaterialDialog.Builder(this)
                .title("標題")
                .content("內容")
                .positiveText("確認")
                .negativeText("取消")
                .neutralText("更多")
                .onAny(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog dialog, DialogAction which) {
                        Toast.makeText(MainActivity.this, which.toString(), Toast.LENGTH_SHORT).show();
                    }
                })
                .show();

這裡寫圖片描述

7、CheckBox

這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("標題")
                .content("內容")
                .positiveText("確認")
                .checkBoxPromptRes(R.string.dont_ask_again, false, null)
                .onAny(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog dialog, DialogAction which) {
                        Toast.makeText(MainActivity.this, dialog.isPromptCheckBoxChecked()+"", Toast.LENGTH_SHORT).show();
                    }
                })
                .show();

8、List Dialogs

這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("標題")
                .positiveText("確認")
                .negativeText("取消")
                .items(R.array.items)
                .itemsCallback(new MaterialDialog.ListCallback() {
                    @Override
                    public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                        Toast.makeText(MainActivity.this, which+"", Toast.LENGTH_SHORT).show();
                    }
                })
                .show();

9、單選列表

這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("標題")
                .positiveText("確認")
                .items(R.array.items)
                .itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
                    @Override
                    public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                        ...
                        return true;
                    }
                })
                .show();

10、多選列表

這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("標題")
                .positiveText("確認")
                .items(R.array.items)
                .itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() {
                    @Override
                    public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
                        return true;
                    }
                })
                .show();

11、指定id的列表

這裡寫圖片描述

<resources>
    <string-array name="items">
        <item>aa</item>
        <item>bb</item>
        <item>cc</item>
    </string-array>
    <integer-array name="itemIds">
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </integer-array>
</resources>
        new MaterialDialog.Builder(this)
                .title("標題")
                .positiveText("確認")
                .items(R.array.items)
                .itemsIds(R.array.itemIds)
                .itemsCallback(new MaterialDialog.ListCallback() {
                    @Override
                    public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                        Toast.makeText(MainActivity.this, which + ": " + text + ", ID = " + view.getId(), Toast.LENGTH_SHORT).show();
                    }
                })
                .show();

12、自定義View

這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("Title")
                .customView(R.layout.item_cart, true)
                .positiveText("CHOOSE")
                .show();

13、輸入框

這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("Title")
                .content("內容")
                .inputType(InputType.TYPE_CLASS_TEXT)
                .input("說點什麼", null, new MaterialDialog.InputCallback() {
            @Override
            public void onInput(MaterialDialog dialog, CharSequence input) {
                ToastUtils.showToast(MainActivity.this, input);
            }
        })
                .positiveText("確定")
                .show();

14、限制有效字數

這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("Title")
                .inputRangeRes(2, 20, R.color.warning)
                .inputType(InputType.TYPE_CLASS_TEXT)
                .input("說點什麼", null, new MaterialDialog.InputCallback() {
            @Override
            public void onInput(MaterialDialog dialog, CharSequence input) {
                ToastUtils.showToast(MainActivity.this, input);
            }
        })
                .positiveText("確定")
                .show();

15、不確定的進度條

這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("Progress")
                .content("Please Wait...")
                .progress(true, 0)
                .show();

水平
這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("Progress")
                .content("Please Wait...")
                .progress(true, 0)
                .progressIndeterminateStyle(true)
                .show();

16、確定的進度條

這裡寫圖片描述

public class MainActivity extends AppCompatActivity {
    private Thread thread;

    private void startThread(Runnable run) {
        if (thread != null) {
            thread.interrupt();
        }
        thread = new Thread(run);
        thread.start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void click(View view) {
        new MaterialDialog.Builder(this).title("Progress").content("please wait...").contentGravity(GravityEnum.CENTER).progress(false, 150, true).cancelListener(dialog -> {
            if (thread != null) {
                thread.interrupt();
            }
        }).showListener(dialogInterface -> {
            final MaterialDialog dialog = (MaterialDialog) dialogInterface;
            startThread(() -> {
                while (dialog.getCurrentProgress() != dialog.getMaxProgress() && !Thread.currentThread().isInterrupted()) {
                    if (dialog.isCancelled()) {
                        break;
                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        break;
                    }
                    dialog.incrementProgress(1);
                }
                runOnUiThread(() -> {
                    thread = null;
                    dialog.setContent(getString(R.string.md_done_label));
                });
            });
        }).show();
    }
}

17、樣式

Basics

暗色主題
這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("Progress")
                .content("Please Wait...")
                .theme(Theme.DARK)
                .progress(true, 0)
                .progressIndeterminateStyle(true)
                .show();

Colors

可以設定顏色的有以下這些

        .titleColorRes(R.color.material_red_500)
        .contentColor(Color.WHITE) // notice no 'res' postfix for literal color
        .linkColorAttr(R.attr.my_link_color_attr)  // notice attr is used instead of none or res for attribute resolving
        .dividerColorRes(R.color.material_pink_500)
        .backgroundColorRes(R.color.material_blue_grey_800)
        .positiveColorRes(R.color.material_red_500)
        .neutralColorRes(R.color.material_red_500)
        .negativeColorRes(R.color.material_red_500)
        .widgetColorRes(R.color.material_red_500)
        .buttonRippleColorRes(R.color.material_red_500)

部分使用示例
這裡寫圖片描述

        new MaterialDialog.Builder(this)
                .title("Progress")
                .content("Please Wait...")
                .titleColorRes(R.color.white)
                .contentColor(Color.WHITE)
                .backgroundColorRes(R.color.alpha_black_80)//#80000000
                .progress(true, 0)
                .show();

Selectors

用來改變drawable被點選或聚焦時的狀態

        .btnSelector(R.drawable.custom_btn_selector)
        .btnSelector(R.drawable.custom_btn_selector_primary, DialogAction.POSITIVE)
        .btnSelectorStacked(R.drawable.custom_btn_selector_stacked)
        .listSelector(R.drawable.custom_list_and_stackedbtn_selector)

Gravity

改變重力位置

        .titleGravity(GravityEnum.CENTER)
        .contentGravity(GravityEnum.CENTER)
        .btnStackedGravity(GravityEnum.START)
        .itemsGravity(GravityEnum.END)
        .buttonsGravity(GravityEnum.END)