类如何调用具有由参数定义的名称的方法?

我是Java新手,所以我不太了解。 我创建了一个类,该类在收到正输入时将激活一个方法。看起来像这样。


public class ExampleDialog extends AppCompatDialogFragment {

    private static final String ARGUMENT_TITLE = "title";
    private static final String ARGUMENT_POSITIVE = "positive";
    private static final String ARGUMENT_MESSAGE = "message";
    private static final String ARGUMENT_POSITIVE_TEXT = "positive_text";
    private static final String ARGUMENT_NEGATIVE = "negative";
    private static final String ARGUMENT_NEGATIVE_TEXT = "negative_text";
    private static final String ARGUMENT_TAG_ID = "tag id";

    private ExampleDialogListener listener;

    private String title;
    private String message;
    private String positive;
    private String positivetext;
    private String negative;
    private String negativetext;
    private String tagid;

    public static ExampleDialog newInstance(String title, String message, String positive,
                                            String positivetext, String negative, String negativetext,) {
        Bundle args = new Bundle();
        // Store all arguments into bundle.
        args.putString(ARGUMENT_TITLE, title); //save as name ARGUMENT_TITLE, value is the user input title, shove inside a bundle called args
        args.putString(ARGUMENT_POSITIVE, positive);
        args.putString(ARGUMENT_MESSAGE, message);
        args.putString(ARGUMENT_POSITIVE_TEXT, positivetext);
        args.putString(ARGUMENT_NEGATIVE, negative);
        args.putString(ARGUMENT_NEGATIVE_TEXT, negativetext);
        ExampleDialog fragment = new ExampleDialog();
        fragment.setArguments(args);
        return fragment; 
    }


    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState)  {

        title = getArguments().getString(ARGUMENT_TITLE); 
        positive = getArguments().getString(ARGUMENT_POSITIVE);
        message = getArguments().getString(ARGUMENT_MESSAGE);
        positivetext = getArguments().getString(ARGUMENT_POSITIVE_TEXT);
        negative = getArguments().getString(ARGUMENT_NEGATIVE);
        negativetext = getArguments().getString(ARGUMENT_NEGATIVE);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(title)
                .setMessage(message)
                .setNegativeButton(negative, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(getContext(), negativetext, Toast.LENGTH_SHORT);
                        toast.show();
                    }
                })
                .setPositiveButton(positive, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(getContext(), positivetext, Toast.LENGTH_SHORT);
                        toast.show();
                        listener.onYesClicked(); 
                    }
                });
        return builder.create();
    }

    public interface ExampleDialogListener {
        void onYesClicked();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        try {
            listener = (ExampleDialogListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + "must implement ExampleDialogListener");
        }
    }

要使用此类,我必须打电话做以下事情

ExampleDialog overwritedialog = ExampleDialog.newInstance("Alert", "This will overwrite current save",
                                                            "Overwrite", "Override successful","Cancel",
                                                            "Cancelled");
                overwritedialog.show(getSupportFragmentManager(), "example dialog");

and then implement a method onYesClicked which contains what I want to happen when the postive buttons is pressed.

However, I want to be able to use multiple ExampleDialog, but the class currently only listens to one method, which is onYesClicked. I want to be able to be able to use the class as

ExampleDialog overwritedialog = ExampleDialog.newInstance("Alert", "This will overwrite current save",
                                                            "Overwrite", "Override successful","Cancel",
                                                            "Cancelled", "DialogA");
overwritedialog.show(getSupportFragmentManager(), "example dialog");

such that when I can instead call the method DialogA instead of onYesClicked. What should I do?

评论