DialogFragment允许点击后面的活动

时间:2016-11-10 12:43:38

标签: android android-dialogfragment

我从ActivityA启动了一个DialogFragment,当我点击没有元素的DailogFragment的白色背景时,ClickA事件发生在后台的ActivityA上

public class FilterDialog extends DialogFragment{

    private static final String TAG = "FilterDialog";
    Calendar myCalendar = Calendar.getInstance();
    private String stringDate;


    DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
            monthOfYear++;
            stringDate = dayOfMonth+"-"+ monthOfYear +"-"+year;
        }

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.dialog_filter, container, false);

        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        toolbar.setTitle("Filter");
        Button datePicker = (Button) rootView.findViewById(R.id.datePicker);
        View.OnClickListener datePickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(v.getId() == R.id.datePicker){
                    new DatePickerDialog(v.getContext(), date, 
                                   myCalendar.get(Calendar.YEAR),
                                   myCalendar.get(Calendar.MONTH),
                                   myCalendar.get(Calendar.DAY_OF_MONTH)).show();

                }
            }
        };
        datePicker.setOnClickListener(datePickListener);

        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

        ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setHomeAsUpIndicator(android.R.drawable.ic_menu_close_clear_cancel);
        }
        return rootView;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onStart()
    {
        super.onStart();
        final AlertDialog d = (AlertDialog)getDialog();
        if(d != null)
        {
            Button positiveButton = (Button) d.getButton(Dialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Boolean wantToCloseDialog = false;

                    if(wantToCloseDialog)
                        d.dismiss();

                }
            });
        }
    }
}

这是我的DialogFragment,用于显示我使用的对话碎片

FragmentManager fragmentManager = getSupportFragmentManager();
FilterDialog newFragment = new FilterDialog();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.replace(android.R.id.content, newFragment).addToBackStack(null).commit();

screenshot

如上面的屏幕截图所示,如果我点击任何突出显示的颜色,则会在ActivityA上进行单击,该活动位于后台且不可见。任何人都可以帮我禁用它。

2 个答案:

答案 0 :(得分:13)

为该对话框的根布局设置android:clickable=true

答案 1 :(得分:0)

您已在setCancelable(false);方法中设置了onCreateView,位于inflate行旁边,如下所示

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.dialog_filter, container, false);
        setCancelable(false);
}

希望这有用:)