2

我阅读了 DialogFragment,并像这样合二为一。

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class MyDialogFragment extends DialogFragment {
public static MyDialogFragment newInstance(int title) {
    MyDialogFragment frag = new MyDialogFragment();
    Bundle args = new Bundle();
    args.putInt("title", title);
    frag.setArguments(args);
    return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int title = getArguments().getInt("title");

    return new AlertDialog.Builder(getActivity())
            .setIcon(R.drawable.plus_icon)
            .setTitle(title)
            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        AddExerciseFragment.doPositiveClick();
                    }
                }
            )
            .setNegativeButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                       AddExerciseFragment.doNegativeClick();
                    }
                }
            )
            .create();
}

}

在另一个 SherlockFragment 中,我接下来制作:

public void doPositiveClick() {


}

public  void doNegativeClick() {


}
void showDialog() {
    DialogFragment newFragment = MyDialogFragment.newInstance(
            R.string.name);
    newFragment.show(getFragmentManager(), "dialog");
}

但是方法 doPositiveClick(),doNegativeClick() 想要是静态的——这对我不利。

public void doPositiveClick() {
    DialogFlag = 0;
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
    // dialog.cancel();

}


@Override
public void onActivityResult(int requestCode, int resultCode,
        Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    String path = null;
    if (DialogFlag == 0) {


        switch (requestCode) {
        case GALLERY_REQUEST:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = imageReturnedIntent.getData();
                path = getRealPathFromURI(selectedImage);
                Log.d("myLogs", path);
                if (btnID == 1) {
                    pathOne = path;
                    Bitmap bmImg = BitmapFactory.decodeFile(pathOne);
                    ivOne.setImageBitmap(bmImg);
                    one = bmImg;
                } else {
                    pathTwo = path;
                    Bitmap bmImg = BitmapFactory.decodeFile(pathTwo);
                    ivTwo.setImageBitmap(bmImg);
                    two = bmImg;
                }

            }
        }
    }
    if (DialogFlag == 1) {
        Uri uri;
        if (requestCode == CAMERA_RESULT) {
            Cursor cursor = getActivity().getContentResolver().query(
                    Media.EXTERNAL_CONTENT_URI,
                    new String[] { Media.DATA, Media.DATE_ADDED,
                            MediaStore.Images.ImageColumns.ORIENTATION },
                    Media.DATE_ADDED, null, "date_added ASC");
            if (cursor != null && cursor.moveToFirst()) {
                do {
                    uri = Uri.parse(cursor.getString(cursor
                            .getColumnIndex(Media.DATA)));
                    path = uri.toString();
                } while (cursor.moveToNext());
                cursor.close();
            }
            Log.d("myLogs", path);
            if (btnID == 1) {
                pathOne = path;
                Bitmap bmImg = BitmapFactory.decodeFile(pathOne);
                ivOne.setImageBitmap(bmImg);
                one = bmImg;
            } else {
                pathTwo = path;
                Bitmap bmImg = BitmapFactory.decodeFile(pathTwo);
                ivTwo.setImageBitmap(bmImg);
                two = bmImg;
            }

        }
    }
}
4

2 回答 2

4

在您的 Fragment 课程中

声明如下

public static final int DIALOG_FRAGMENT = 1;
public static final int RESULT_OK = 101;

然后

   DialogFragment newFragment = MyDialogFragment.newInstance(
        R.string.name);
   newFragment.setTargetFragment(SherLockFragmentName.this, DIALOG_FRAGMENT); 
   newFragment.show(getFragmentManager(), "dialog");

然后在对话框片段中

    .setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    Intent i =getActivity().getIntent();
                    i.putExtra("key", true);
                    getTargetFragment().onActivityResult(getTargetRequestCode(), 101, i);
                }
            }
        )
        .setNegativeButton("NO",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                   //AddExerciseFragment.doNegativeClick();
                    Intent i =getActivity().getIntent();
                    i.putExtra("key", false);

                    getTargetFragment().onActivityResult(getTargetRequestCode(), 101, i);
                }
            }
        )

onActivityResul然后在 Fragment 类中覆盖t

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
            case DIALOG_FRAGMENT:

                if (resultCode == RESULT_OK) {
                    boolean check = data.getBooleanExtra("key", true);
                    if(check)
                    {
                        dopositiveClick();
                    }
                    else
                    {
                        donegativeClick();
                    }
                } 
                break;
        }
    }
于 2013-07-31T10:33:07.517 回答
0

如果您不想要静态访问,这里有另一种选择:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");

final AddExerciseFragment aef = new AddExerciseFragment(SomeParameters...);

return new AlertDialog.Builder(getActivity())
            .setIcon(R.drawable.plus_icon)
            .setTitle(title)
            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton){
                        aef.doPositiveClick();
                    }
//and so on
于 2013-07-31T09:39:02.667 回答