Layout in AlertDialog

0

I have this AlertDialog which shows on the screen another layout , in this layout has several EditText and a Button . I would like to know how I can do to get the text of each of the edittext when I click on ok or the button that has layout that will appear in AlertDialog . I tried to LayoutInflanter but could not. See:

Context context = getBaseContext();
LayoutInflater inflant = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
final LinearLayout layout = (LinearLayout) inflant.inflate(R.layout.main, null);

AlertDialog.Builder dialogo = new AlertDialog.Builder(MainActivity.this);

dialogo.setTitle("Informe o Nome");
dialogo.setView(R.layout.main);
dialogo.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface di, int e) {
        ettext = (EditText) layout.findViewById(R.id.ettext);

        Toast.makeText(getBaseContext(), ettext.getText(), Toast.LENGTH_SHORT).show();
    }
});
dialogo.show();
    
asked by anonymous 08.08.2017 / 04:29

2 answers

3

Use LayoutInflater to get a View that represents your xml file.

LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.main, null, false);

From the view, take the instances of your EditText and the Button:

EditText edtText= (EditText) view.findViewById(R.id.edit_text);
Button btn = (Button) view.findViewById(R.id.button);

If you need to use LinearLayout, assign an id to it and take the instance as shown above. Finally, add the View to your Dialog:

 dialogo.setView(view);
    
08.08.2017 / 14:00
0

This worked well

public void Dialog() {

    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final LayoutInflater inflater = LayoutInflater.from(getContext());
    View view = inflater.inflate(R.layout.dialog_esqueci_senha, null, false);
    final EditText editText = view.findViewById(R.id.et_dialog_esqueci_senha_email);
    builder.setView(view)

            .setPositiveButton("Enviar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    Toast.makeText(getContext(), ""+editText.getText().toString(), Toast.LENGTH_SHORT).show();
                }
            })
            .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            });
    builder.show();
}
    
21.02.2018 / 16:27