我正在编写一个非常简单的对话框片段,它显示3个选项并将选择返回到父片段。做出选择后,我希望对话框解除自己。下面是我写的onCreateDialogMethod,调用了CheckOutDialogFragment.this.dismiss()。我也尝试过dismiss()和其他一些方法。有什么建议?
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
activity = this.getActivity();
Bundle args = getArguments();
final String comment = args.getString(CheckInOutFragment.COMMENT);
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(activity);
dialogAdapter = new CheckOutDialogAdapter(this.getActivity());
final Intent data = new Intent();
myAlertDialog.setSingleChoiceItems(dialogAdapter, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int selection){
ActivityModel.CheckOutType type = dialogAdapter.getActivityTypeForIndex(selection);
data.putExtra(CheckInOutFragment.COMMENT, comment);
data.putExtra(CHOICE, type);
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, data);
CheckOutDialogFragment.this.dismiss();
//TODO: figure out why this won't dismiss
}
});
return myAlertDialog.create();
}
答案 0 :(得分:2)
而不是:
CheckOutDialogFragment.this.dismiss();
应该只是:
dismiss();
答案 1 :(得分:2)
原来我的IDE需要重启,我的项目没有正确构建。 CheckOutDialogFragment.this.dismiss()和dismiss()都有效。