android-fragments 将输入从对话框片段发送到ProfileFragment

eufgjt7s  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(169)

我知道有类似的问题,但我找不到一个答案来解决我的问题。
我有一个DialogUpdateEmail,我想从ProfileFragment打开它。在对话框中,我想输入我的新电子邮件,并将其发送到我的ProfileFragment,以便在数据库中也更改它。
ProfileFragment.java :

import androidx.fragment.app.Fragment;
public class ProfileFragment extends Fragment implements SendInputEmail {

    public static final int PROFILE_FRAGMENT = 1;
    private static final String TAG = "ProfileFragment";
    private TextView TVHello, TVUsernameMessage, TVusernameinfo, TVemailinfo, TVbirthdate;

    public void sendInput(String input) {
        Log.d(TAG, "sendInput: found incoming input: " + input);

        TVemailinfo.setText(input);
    }
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_profile, container, false);
        Button Bsignout = (Button) v.findViewById(R.id.signoutbutton);
        Button Beditusername = (Button) v.findViewById(R.id.editusernamebutton);
        Button Beditemail = (Button) v.findViewById(R.id.editemailbutton);

        Beditemail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: opening email dialog");
                DialogUpdateEmail dialog = new DialogUpdateEmail();
         dialog.setTargetFragment(ProfileFragment.this,PROFILE_FRAGMENT);
                dialog.show(getActivity().getFragmentManager(), "DialogUpdateEmail");
            }
        });
return v;
}

DialogUpdateEmail.java :

public class DialogUpdateEmail extends DialogFragment implements SendInputEmail {

    private static final String TAG = "DialogUpdateEmail";
SendInputEmail mHost = (SendInputEmail)getTargetFragment();

    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.popup, container, false);
        EditText UpdateEmail = (EditText) view.findViewById(R.id.emailinfoupdate);
        Button Beditemail = (Button) view.findViewById(R.id.updatesavebutton);
        Button Bcancelemail = (Button) view.findViewById(R.id.updatecancelbutton);


        Beditemail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: capturing input.");

                String input = UpdateEmail.getText().toString();
                Log.d(TAG, "input : "+input);
                mHost.sendInput(input);

                getDialog().dismiss();

            }
        });
return view ; 
}
    public void onAttach(Context context) {
        super.onAttach(context);
        try{
            mHost = (SendInputEmail) getTargetFragment();
        }catch (ClassCastException e){
            Log.e(TAG, "onAttach: ClassCastException : " + e.getMessage() );
        }
    }
    @Override
    public void sendInput(String input) {

    }
}

发送输入电子邮件接口:

public interface SendInputEmail {
    void sendInput(String input);
}

我的问题是,当我尝试在ProfileFragment中使用setTargetFragment时,出现了一个错误。它说Profile Fragment不是一个Fragment,我真的不知道为什么。
第一次

t2a7ltrp

t2a7ltrp1#

来自here中的文档:

public class DialogUpdateEmail extends DialogFragment {

    private DialogEditListener listener;

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = requireActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        View view = inflater.inflate(R.layout.popup, null);
        builder.setView(view);

        EditText UpdateEmail = (EditText) view.findViewById(R.id.emailinfoupdate);
        Button Beditemail = (Button) view.findViewById(R.id.updatesavebutton);
        Button Bcancelemail = (Button) view.findViewById(R.id.updatecancelbutton);

        Beditemail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDialogEditClick(UpdateEmail.getText().toString());
                    DialogUpdateEmail.this.getDialog().dismiss();
                }
            }
        });

        Bcancelemail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUpdateEmail.this.getDialog().cancel();
            }
        });

        return builder.create();
    }

    public interface DialogEditListener {
        void onDialogEditClick(String email);
    }

    public void setListener(DialogEditListener listener) {
        this.listener = listener;
    }
}

我们使用observer/listener模式将电子邮件从对话框发送到片段。
在你的ProfileFragment中实现DialogEditListener并订阅它来监听对话框中的点击按钮,如下所示:

public class ProfileFragment extends Fragment 
        implements SendInputEmail, DialogUpdateEmail.DialogEditListener {

    public static final int PROFILE_FRAGMENT = 1;
    private static final String TAG = "ProfileFragment";
    private TextView TVHello, TVUsernameMessage, TVusernameinfo, TVemailinfo, TVbirthdate;

    public void sendInput(String input) {
        Log.d(TAG, "sendInput: found incoming input: " + input);

        TVemailinfo.setText(input);
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_profile, container, false);
        Button Bsignout = (Button) v.findViewById(R.id.signoutbutton);
        Button Beditusername = (Button) v.findViewById(R.id.editusernamebutton);
        Button Beditemail = (Button) v.findViewById(R.id.editemailbutton);

        Beditemail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: opening email dialog");
                //show your dialog
                DialogUpdateEmail dialogUpdateEmail = new DialogUpdateEmail();
                dialogUpdateEmail.setListener(ProfileFragment.this);
                dialogUpdateEmail.show(getActivity().getSupportFragmentManager(), "DialogUpdateEmail");
            }
        });
        return v;
    }

    @Override
    public void onDialogEditClick(String email) {
        //use your email here
        Toast.makeText(getContext(), "My email: " + email, Toast.LENGTH_SHORT).show();
    }
}

相关问题