Android:从 fragment/activity 为自定义对话框中的视图调用 onClick

Android: Call onClick from fragment/activity for views within Custom Dialog

我创建了一个自定义对话框,其中包含多个视图。 单击这些视图后,我想启动结果活动,例如相机、图库等。

自定义对话框

public class CustomDialog extends BottomBaseDialog {
    public static LinearLayout ll_camera;
    public static LinearLayout ll_gallery;

    public CustomDialog(Context context, View animateView) {
        super(context, animateView);
    }

    @Override
    public View onCreateView() {
        View inflate = View.inflate(context, R.layout.dialog_custom, null);

        ll_camera = ViewFindUtils.find(inflate, R.id.camera_linear_layout);
        ll_gallery = ViewFindUtils.find(inflate, R.id.gallery_linear_layout);

        return inflate;
    }

    @Override
    public boolean setUiBeforShow() {
        ll_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // OPEN INTENT FOR CAMERA
                dismiss();
            }
        });
        ll_gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // OPEN INTENT FOR GALLERY
                dismiss();
            }
        });

        return false;
    }
}

然后在我的片段中,我通过

显示了这个对话框
IOSTaoBaoDialog dialog = new IOSTaoBaoDialog(getActivity(), AddActivity.drawerLayout);
dialog.show();

如何从 Fragment 中为相机和图库线性布局视图调用 onClick? 我还需要将活动的结果返回到片段中,以便我可以对其进行处理。 请提出建议。

我进行了大量搜索,发现了使用接口的建议,但是,我不清楚它是如何工作的。

您应该使用 DialogFragmentlook at this answer. 以下是该答案中的两行重要内容:

在调用片段中:

dialog.setTargetFragment(this, YES_NO_CALL);

在 DialogFragment 中:

getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);

基本上,当您调用DialogFragment时,您需要使用请求代码调用setTargetFragment。然后在您的片段中,您处理 onActivityResult.

中的响应
IOSTaoBaoDialog dialog = new IOSTaoBaoDialog(getActivity(), AddActivity.drawerLayout);
dialog.show();

改为

IOSTaoBaoDialog dialog = new IOSTaoBaoDialog(getparent(), AddActivity.drawerLayout);
dialog.show();

如果这不行那就试试

IOSTaoBaoDialog dialog = new IOSTaoBaoDialog(getActivity().getparent(), AddActivity.drawerLayout);
dialog.show();

IOSTaoBaoDialog dialog = new IOSTaoBaoDialog(getparent().getActivity(), AddActivity.drawerLayout);
    dialog.show();

对于 startActivityForResult()

IF(getparent() == null) 
{
 startActivityForResult();
}else
{
 getparent().startActivityForResult();
} 

我找到了解决方案,因为如果其他人也遇到同样的情况:

我将调用片段的实例传递给了对话框。然后在对话框中,我调用了 fragment.startActivityForResult() 方法。所以当收到结果时,它被发送到片段的onActivityResult()方法。

密码是:

对话:

public class SelectApplicationDialog extends BottomBaseDialog {
    public static LinearLayout ll_camera;
    public static LinearLayout ll_gallery;

    Fragment fragment;

    public SelectApplicationDialog(Context context, View animateView, Fragment fragment) {
        super(context, animateView);
        this.fragment = fragment;
    }

    @Override
    public View onCreateView() {
        View inflate = View.inflate(context, R.layout.dialog_select_application, null);

        ll_camera = ViewFindUtils.find(inflate, R.id.camera_linear_layout);

        ll_gallery = ViewFindUtils.find(inflate, R.id.gallery_linear_layout);

        return inflate;
    }

    @Override
    public boolean setUiBeforShow() {
        ll_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File externalStorageFile = new File(imagePath);

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                AddCourseFragment.imageUri = Uri.fromFile(externalStorageFile);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, AddCourseFragment.imageUri);
                fragment.startActivityForResult(intent, 1);
                dismiss();
            }
        });
        ll_gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                fragment.startActivityForResult(pickPhoto, 2);
                dismiss();
            }
        });

        return false;
    }
}

调用片段:

public void openUploadImageDialog() {
    SelectApplicationDialog dialog = new SelectApplicationDialog(getContext(), 
            AddActivity.addLinearLayout, AddCourseFragment.this);
    dialog.show();
}