使用NexusDialog在片段中创建自定义元素

时间:2018-01-02 10:00:31

标签: java android android-fragments

我想使用nexusDialog Library(https://github.com/dkharrat/NexusDialog)通过片段创建自定义元素 我使用开发人员给出的示例创建了一个自定义元素 但我不知道如何在片段中生成那些自定义组件而不是活动。我想将数据传递到编辑文本,并使用库

获取edittext的值

这是我的代码

自定义组件

public class EditTextComp extends FormElementController {

    EditText editText = null;

    TextView textView = null;

    public EditTextComp(Context context) {
        super(context, null);

    }


    @Override
    protected View createView() {
        return createComponent();
    }

    @Override
    public void refresh() {

    }

    @Override
    public void setError(String message) {

    }




    private View createComponent() {

        LayoutInflater inflater = LayoutInflater.from(getContext());
        return inflater.inflate(R.layout.compound_comp_edit_text, null);

    }

    public EditText getEditText() {
        return (EditText) getView().findViewById(R.id.editText);
    }

}

1 个答案:

答案 0 :(得分:1)

我能够使用图书馆FormFragment以这种方式做到这一点

儿童片段

public  class fragmentFragment extends FormFragment {
    public static final String FIRST_NAME = "firstName";
    public static final String LAST_NAME = "lastName";
    public static final String GENDER = "gender";
    public static final String HOBBIES = "hobbies";

    public boolean validate() {
        getFormController().resetValidationErrors();
        if (getFormController().isValidInput()) {
            Object firstName = getModel().getValue(FIRST_NAME);
            Object lastName = getModel().getValue(LAST_NAME);
            Object gender = getModel().getValue(GENDER);
            Object favColor = getModel().getValue(HOBBIES);

            String msg = "First name: " + firstName + "\n"
                    + "Last name: " + lastName + "\n"
                    + "Gender: " + gender + "\n"
                    + "Hobbies: " + favColor + "\n";
            MessageUtil.showAlertMessage("Field Values", msg, getActivity());
        } else {
            getFormController().showValidationErrors();
        }
        return true;
    }


    @Override
    public void initForm(FormController controller) {
        Context ctxt = getContext();
        FormSectionController section = new FormSectionController(ctxt, "Personal Info");
        section.addElement(new EditTextController(ctxt, FIRST_NAME, "First name"));
        section.addElement(new EditTextController(ctxt, LAST_NAME, "Last name"));


        controller.addSection(section);
    }
}

父级片段

  btn= view.findViewById(R.id.btn_in_xml);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                FragmentManager fm = getFragmentManager() ;
                fragmentFragment formFragment;

                Fragment retainedFragment = fm.findFragmentByTag(FORM_FRAGMENT_KEY);
                if (retainedFragment != null && retainedFragment instanceof childFragment) {
                    formFragment = (childFragment) retainedFragment;
                } else {
                    formFragment = new childFragment();
                    fm.beginTransaction()
                            .replace(R.id.flContent, formFragment, FORM_FRAGMENT_KEY)
                            .commit();
                }


            }
        });