[Android] Dialog篇

一、 AlertDialog

  1. 简单Dialog:
Dialog dialog = new AlertDialog.Builder(this)
                .setTitle("dialog")
                .setIcon(R.drawable.ic_launcher_background)
                .setMessage("this is message")
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("agree", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //
                    }
                })
                .show();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);

三键:再setNeutralButton
setCancelable(false)设置dialog不可取消
setCanceledOnTouchOutside(false)设置点击蒙层不可取消
两个都是false,则会屏蔽返回键,如需对返回键进行事件处理: setOnKeyListener
在dialog消失时进行处理: setOnDismissListener

  1. 列表Dialog
    从列表中选择点击事件
String[] items = {"itema", "itemb", "itemc", "itemd"};
        Dialog dialog = new AlertDialog.Builder(this)
                .setTitle("dialog")
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "click "+items[which], Toast.LENGTH_SHORT).show();
                    }
                })
                .show();

int: R.array.test
在这里插入图片描述

  1. 单选/多选
String[] items = {"itema", "itemb", "itemc", "itemd"};
        Dialog dialog = new AlertDialog.Builder(this)
                .setTitle("dialog")
                .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "click " + which, Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
String[] items = {"itema", "itemb", "itemc", "itemd"};
        boolean[] checked = {true, true, false, false};// 鍒濆閫変腑
        Dialog dialog = new AlertDialog.Builder(this)
                .setTitle("dialog")
                .setMultiChoiceItems(items, checked, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked) {
                            Toast.makeText(MainActivity.this, "checked "+items[which], Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "not check", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .show()
  1. 输入框Dialog
EditText editText = new EditText(this);
        Dialog dialog = new AlertDialog.Builder(this)
                .setTitle("dialog")
                .setView(editText)
                .show();

二、自定义dialog
自定义xml布局文件,setView引入;如上述输入框Dialog

View dialogView = LayoutInflater.from(this).inflate(R.layout.custom_dialog_layout,null);
        Dialog dialog = new AlertDialog.Builder(this)
                .setView(dialogView)
                .show();

or
继承Dialog类,setContentView完全自定义Dialog

public abstract class News extends Dialog {

    public News(@NonNull Activity activity, CharSequence text) {
        super(activity, R.style.News);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(false);
        setContentView(R.layout.dialog_back);
    }
}

附上一个常用的dialog style

<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="CustomDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>
</resourc

三、DialogFragment
优点:在配置变化(屏幕旋转)时自动恢复
create:

public class TestDialog extends DialogFragment {//或者onCreateView创建view
    Dialog dialog;

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        dialog = new AlertDialog.Builder(requireContext())
                .setTitle("dialog")
                .setMessage("this is a dialog")
                .show();
        return dialog;
    }
}

可以通过onCreateView进行自定义,所返回的view会自动添加入dialog
show:

DialogFragment fr = new TestDialog();
fr.show(getSupportFragmentManager(), "tag");
// FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// transaction.add(fr, "tag").commit();

同样有dismiss(),dismiss()等方法

你可能感兴趣的:(android,android,studio,java)