AlertDialog点击positiveButton, 弹框不消失

AlertDialog弹出后,默认情况下点击它的按钮会使得dialog消失, 怎么让点击按钮后,不消失呢?

思路创建AlertDialog时,设置它的positiveButton,并且将监听设为null

 AlertDialog dialog =new  AlertDialog.Builder(getActivity(),R.style.Dialog_Fullscreen).setView(view)
                            .setPositiveButton("保存",null)
                            .setNegativeButton("返回",null).create();

dialog.show()后,再为dialog设置按钮监听:

                  dialog.show();   //dialog显示后才能找到 AlertDialog.BUTTON_POSITIVE
                  dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //Toast.makeText(getActivity(),"保存",Toast.LENGTH_SHORT).show();
                            saveImg(position);  
                        }
                    });


如此一来,点击确定按钮,dialog不会消失,当然你也可以让它消失,在上面onClick()里面加上dialog.dismiss()就可以!

你可能感兴趣的:(Android知识碎碎片)