我已经成功设置了我的 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;
}
}