我有一个场景,其中显示一个DialogFragment
(使用getSupportFragmentManager()
),显示值列表和“添加”按钮。如果用户单击添加按钮,我会显示另一个DialogFragment
(使用getChildFragmentManager()
,它由EditText
和Button
组成。如果该值通过验证,用户单击新的Button
,我将它添加到列表中。如果它未通过验证,我会显示另一个DialogFragment
,显示错误信息和一个“OK”按钮,我希望当按下“OK”按钮时,用户仍然会看到DialogFragment
和EditText
和Button
,以便他们可以查看但错误信息DialogFragment
中的“确定”按钮被按下时,对话框将被解除,并且只显示原始的DialogFragment
(带有值列表的那个)Android Multiple Nested DialogFragment
第三个被解雇后,我会如何保持第二个DialogFragment
仍然显示?
编辑:这里有一些代码段:
//clicking this button shows the first ("outer") fragment
findViewById(R.id.voicemail_notifications_email_addresses).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AddressListDialog.newInstance(
line, AddressListDialog.VOICEMAIL_NOTIFICATIONS_EMAIL)
.show(((FragmentActivity)getContext()).getSupportFragmentManager(), "dialog");
}
});
public static class AddressListDialog extends DialogFragment {
public AddressListDialog() {}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final View v = LayoutInflater.from(getActivity()).inflate(R.layout.vm_address_list_dialog, null);
ListView lv = (ListView) v.findViewById(R.id.voicemail_notifications_address_list);
final AddressAdapter adapter = new AddressAdapter();
lv.setAdapter(adapter);
v.findViewById(R.id.voicemail_add_address_button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//first child fragment
AddAddressDialog.newInstance(getType()).setAdapter(adapter)
.show(getChildFragmentManager(), "dialog");
}
});
Dialog d = new AlertDialog.Builder(getActivity())
.setTitle(titleRes)
.setView(v)
.setPositiveButton(R.string.global_dialog_pos, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}
)
.create();
return d;
}
private class AddressAdapter extends BaseAdapter {
.
.
.
public boolean add(LineVoicemailInfo.VmContact contact) {
if(addresses.size() >= 3) {
return false;
}
else if(!contact.isValid(isEmailType())) {
return true;
}
else {
addresses.add(contact);
notifyDataSetChanged();
return true;
}
}
public static class AddAddressDialog extends DialogFragment {
private AddressAdapter adapter;
public AddAddressDialog() {}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DialogInterface.OnClickListener onPosClick = null;
final View v;
v = LayoutInflater.from(getActivity()).inflate(R.layout.voicemail_add_email_address, null);
onPosClick = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LineVoicemailInfo.VmContact contact = new LineVoicemailInfo.VmContact(((EditText)v.findViewById(R.id.voicemail_add_email_address)).getText().toString());
if(!adapter.add(contact)) {
//second child fragment
BadAddressDialog.newInstance("Not a valid address")
.show(getChildFragmentManager(), "dialog");
}
}
};
Dialog d = new AlertDialog.Builder(getActivity())
.setTitle("Add Address")
.setView(v)
.setCancelable(false)
.setPositiveButton(R.string.voicemail_addresses_add, onPosClick)
.setNegativeButton(R.string.global_dialog_neg, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
})
.create();
setCancelable(false);
return d;
}
}
public static class BadAddressDialog extends DialogFragment {
public BadAddressDialog() {}
public static BadAddressDialog newInstance(String message) {
BadAddressDialog frag = new BadAddressDialog();
Bundle args = new Bundle();
args.putString("message", message);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String message = getArguments().getString("message");
Dialog d = new AlertDialog.Builder(getActivity())
.setTitle("Error adding address")
.setMessage(message)
.setCancelable(false)
.setPositiveButton(R.string.global_dialog_pos, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
})
.create();
setCancelable(false);
return d;
}
}
你介意显示相关的代码吗? – Stan
@Stan查看编辑问题 – Eliezer
我想问题可能是您使用标准的'show'方法来显示对话框。根据Android的消息来源,这种方法并不会在堆叠中添加碎片,因此最顶层的对话框会被解散,中间也会被解散。在调用'show'并将事务添加到backstack(调用'addToBackStack')之前,您应该自己开始trasaction。一个例子显示在[DialogFragment文档页面](http://developer.android.com/reference/android/app/DialogFragment.html)。 – Stan