1

我已经成功设置了我的 Dialog Fragment 类,现在我想从我设置的主要 Fragment 类中调用它。

我曾尝试使用多个代码来调用它,但我不断收到错误和崩溃。

我需要在 onClick 中添加什么来调用我的对话框片段?

提前致谢!

主要片段类:

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

    final View v = inflater.inflate(R.layout.image_detail_fragment,
            container, false);

    mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
    mImageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {


            //Here


        }

    });

    return v;
}

对话片段:

class MyDialogFragment extends DialogFragment {
Context mContext;

public MyDialogFragment() {
    mContext = getActivity();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            mContext);
    alertDialogBuilder.setTitle("Set Wallpaper?");
    alertDialogBuilder.setMessage("Are you sure?");
    // null should be your on click listener
    alertDialogBuilder.setPositiveButton("OK", null);
    alertDialogBuilder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

    return alertDialogBuilder.create();
}

public static MyDialogFragment newInstance() {
    MyDialogFragment f = new MyDialogFragment();
    return f;
}

}
4

4 回答 4

4

这里的解决方案:

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

    final View v = inflater.inflate(R.layout.image_detail_fragment,
            container, false);

    mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
    mImageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
                MyDialogFragment dialog = MyDialogFragment.newInstance();
            dialog.show(getActivity().getFragmentManager(), "MyDialogFragment");
        }

    });

    return v;
}
于 2013-10-09T10:02:44.670 回答
2

尝试以下

FragmentTransaction ft = ((FragmentActivity)getActivity()).getSupportFragmentManager().beginTransaction();
MyDialogFragment dialog = MydialogFragment.newInstance();
dialog.show(ft, "Tag");

这里也是一个很棒的TUTORIAL

于 2013-10-09T10:01:29.023 回答
0

在您的对话框片段中,您已经拥有以下返回对话框片段实例的内容。

public static MyDialogFragment newInstance() {
    MyDialogFragment f = new MyDialogFragment();
    return f;  
}

所以试试下面

DialogFragment newFragment = MyDialogFragment.newInstance();// call the static method
newFragment.show(getActivity().getFragmentManager(), "dialog");

看看文档有一个例子

http://developer.android.com/reference/android/app/DialogFragment.html

于 2013-10-09T10:03:05.110 回答
0

这是我从 FragmentACtivity 中调用的方式

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                ContactsNavi userPopUp = new ContactsNavi();
                userPopUp.show(fragmentManager,"baglantilar");
于 2016-12-26T11:19:42.347 回答