How to get data from a text displayed in the dialog

Asked

Viewed 993 times

2

I am using this library to display dialogs in my application: https://github.com/drakeet/MaterialDialog

With this library it is possible to display in the dialog a layout created, 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 someone shed some light on this or suggest a more appropriate approach?

Thank you!

2 answers

2


Your Fragment you have already "inflated" and is set, just then you search and define the elements through the Ids, and in the action of the positive button seek its values:

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();
  • Perfect. It worked right here =)

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));
                    }
                }
            });

Dialog layout

<?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" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.