我研究了关于片段和片段对话之间的通信,我发现setTargetFragment和接口是一种方法。但是当我尝试setTargetFragment时,我收到了这个错误。
Fragment类型中的方法setTargetFragment(Fragment,int)不适用于参数(SettingsDetailsFragment,int)。我还没有使用片段直到现在,我是android的新手。 所以我有这个片段:
public class SettingsDetailsFragment extends Fragment implements
NoticeDialogFragment.EmailChangedListner {
public void emailUpdateFromDialog()
{
DialogFragment modifyEmailFragment = new ModifyEmailFragment();
//here is where i get the error
modifyEmailFragment.setTargetFragment(this, 0);`enter code here`
modifyEmailFragment.show(getActivity().getFragmentManager(),
"email");
}
}
// class where i have the interface
public class NoticeDialogFragment extends DialogFragment {
public interface EmailChangedListner {
public void emailChanged(String text);
}
}
// the dialogFragment
public class ModifyEmailFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
final View v_iew = inflater.inflate(R.layout.email_dialog, null);
builder.setView(v_iew)
// Add action buttons
.setPositiveButton(R.string.modifyBtn_settings,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText userEmail = (EditText) v_iew
.findViewById(R.id.modifyEmail);
boolean validateEmail = BookMeUtils
.validateEmail(userEmail.getText()
.toString());
if (validateEmail == false) {
BookMeUtils.enterValidEmail(getActivity());
}
if (!(userEmail.getText().toString().length() == 0)) {
if (validateEmail == true) {
Fragment parentFragment = getTargetFragment();
((EmailChangedListner) parentFragment)
.emailChanged(userEmail
.getText().toString());
}
} else {
}
}
})
.setNegativeButton(R.string.cancel_r,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ModifyEmailFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}
答案 0 :(得分:0)
您的片段将设置侦听器并实现EmailChangedListner:
public class SettingsDetailsFragment extends Fragment implements
EmailChangedListner {
public void emailUpdateFromDialog()
{
DialogFragment modifyEmailFragment = new ModifyEmailFragment();
modifyEmailFragment.setListener(SettingsDetailsFragment.this);
modifyEmailFragment.show(getActivity().getFragmentManager(),
"email");
}
}
Dialog Fragment将具有接口和侦听器对象:
// the dialogFragment
public class ModifyEmailFragment extends DialogFragment {
EmailChangedListner listener;
public void setListener(EmailChangedListner listener) {
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
final View v_iew = inflater.inflate(R.layout.email_dialog, null);
builder.setView(v_iew)
// Add action buttons
.setPositiveButton(R.string.modifyBtn_settings,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText userEmail = (EditText) v_iew
.findViewById(R.id.modifyEmail);
boolean validateEmail = BookMeUtils
.validateEmail(userEmail.getText()
.toString());
if (validateEmail == false) {
BookMeUtils.enterValidEmail(getActivity());
}
if (!(userEmail.getText().toString().length() == 0)) {
if (validateEmail == true) {
Fragment parentFragment = getTargetFragment();
listener.emailChanged(userEmail
.getText().toString());
}
} else {
}
}
})
.setNegativeButton(R.string.cancel_r,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ModifyEmailFragment.this.getDialog().cancel();
}
});
return builder.create();
}
public interface EmailChangedListner {
public void emailChanged(String text);
}
}
答案 1 :(得分:0)
检查Fragment
课程中SettingsDetailsFragment
的导入情况。是导入android.support.v4.app.Fragment
还是android.app.Fragment
。
可能是您将Fragment
android.support.v4.app
设置为DialogFragment的目标android.app
。