How to get data from a text displayed in the dialog

2

I'm using this library to display dialogs in my application: link

With this library it is possible to display in the dialog a created layout, but I am not able to capture the user input in the setPositiveButton method.

mMaterialDialog = new MaterialDialog(new ContextThemeWrapper(getActivity(), R.style.MyAlertDialog ) )
        .setTitle("Novo Registro")

        .setPositiveButton("Novo", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // PEGAR DADOS PREENCHIDOS DO FRAGMENT EXIBIDO AQUI
            }
        })

        .setNegativeButton("Voltar", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMaterialDialog.dismiss();
            }
        });
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_new_classroom, null);
        mMaterialDialog.setView(view).show();
        //mMaterialDialog.show();

Can anyone shed light on this or suggest a more appropriate approach?

Thank you!

    
asked by anonymous 05.10.2015 / 17:48

2 answers

2

Your fragment has already "inflated" and is defined, then you simply search for and define the elements through IDs , and the action of the positive :

View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_new_classroom, null);

final EditText etFoo = (EditText) view.findViewById(R.id.etFoo);

mMaterialDialog = new MaterialDialog(new ContextThemeWrapper(getActivity(), R.style.MyAlertDialog ) )
    .setTitle("Novo Registro")
    .setPositiveButton("Novo", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String foo = etFoo.getText().toString();
        }
    })
    .setNegativeButton("Voltar", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mMaterialDialog.dismiss();
        }
    });

mMaterialDialog.setView(view).show();
    
05.10.2015 / 18:37
0

Try this:

private AlertDialog alertDialog;
private LayoutInflater inflat;
private AlertDialog.Builder builder;

 inflat = (LayoutInflater) getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
            layout = inflat.inflate(R.layout.layout_dialog_erro, null);
            builder = new AlertDialog.Builder(getActivity());
            builder.setView(layout);
            alertDialog = builder.create();
            alertDialog.show();
            Button enviar_erro = (Button) layout.findViewById(R.id.enviar_erro);
            final EditText obs_erro = (EditText) layout.findViewById(R.id.obs_erro);
            enviar_erro.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!TextUtils.isEmpty(obs_erro.getText().toString())) {
                        //pegar texto e enviar obs_erro.getText().toString();
                    } else {
                        obs_erro.setError(getResources().getString(R.string.campo_obrigatorio));
                    }
                }
            });

Layout dialog

<?xml version="1.0" encoding="utf-8"?>

<EditText
    android:id="@+id/obs_erro"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/custom_shape_border_gray"
    android:layout_margin="10dp"
    android:gravity="top"
    android:padding="5dp"
    android:hint="@string/descrever_erro"
    android:singleLine="false" />
<Button
    android:id="@+id/enviar_erro"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="@string/enviar"
    android:layout_marginBottom="10dp"
    android:background="@color/verde"
    android:textColor="@android:color/white"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    
05.10.2015 / 18:43