Android:从片段/活动中调用onClick以查看自定义对话框中的视图

时间:2015-09-16 17:54:25

标签: android android-fragments customdialog

我已经创建了一个自定义对话框,其中包含多个视图。 点击这些视图,我想开始结果活动,如相机,图库等。

CustomDialog

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();

如何在片段中调用onClick以获取相机和图库线性布局视图? 我还需要将活动的结果返回到片段中,以便我可以处理它。 请建议。

我已经做了很多搜索,但我遇到了使用接口的建议,但是,我不清楚它是如何工作的。

3 个答案:

答案 0 :(得分:1)

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();
} 

答案 1 :(得分:0)

您应该使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" id="txt" /> <select id="sel"> <option value=2>ajkl</option> <option value=1>abc</option> <option value=2>def</option> <option value=3>ghi</option> <option value=2>jkl</option> <option value=2>jkl</option> <option value=2>ajkl</option> <option value=2>jklaa</option> <option value=2>jklsdd</option> <option value=2>dddsjkl</option> <option value=2>eeeejkl</option> <option value=2>.co.in</option> <option value=2>.co</option> </select>look at this answer.以下是该答案的两个重要部分:

在调用片段时:

DialogFragment

在DialogFragment中:

dialog.setTargetFragment(this, YES_NO_CALL);

基本上,当您致电getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);时,您需要使用请求代码致电DialogFragment。然后在您的片段中,您处理setTargetFragment中的响应。

答案 2 :(得分:0)

我找到了解决方法,因为如果其他人也陷入了同样的境地:

我将调用片段的实例传递给对话框。然后在对话框中,我调用了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();
}