我从ActivityA启动了一个DialogFragment,当我点击DailogFragment中没有元素的白色背景时,点击事件会发生在后台的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,为了显示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();
如上所示,在屏幕截图中,如果我点击任何一个高亮颜色,将会在后台不可见的ActivityA上发生点击事件。有谁能帮我禁用这一功能呢?