user1813375
user1813375

Reputation: 101

How I can get values EditText a Dialog?

I deployed my application on Android.

I put one in my main Activity button, which when you click, you must show a dialogue in which I write text on 2 EditText and pressed an OK button.

My problem is ... How do I get the values of EditText in the events of my main Activity ??

onDialogPositiveClick(...)
onDialogNegativeClick(...)

This is my class for Dialogue DialogAlex.java

public class DialogAlex extends DialogFragment {

    public interface NoticeDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog,int id);
        public void onDialogNegativeClick(DialogFragment dialog,int id);
    }
    NoticeDialogListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);        
        try {     
            mListener = (NoticeDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {     
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.dialog_signin, null)) //This layout contains only two EditText, ET1 and ET2.

                .setPositiveButton("adicionar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        mListener.onDialogPositiveClick(DialogAlex.this,id);
                    }
                })
                .setNegativeButton("cancelar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        mListener.onDialogNegativeClick(DialogAlex.this, id);
                    }
                });
        return builder.create();
    }
}

and this my Activity Main.

...
@Override
    public boolean onOptionsItemSelected(MenuItem item) {       
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.telefono)
        {
            showNoticeDialog();  //call dialog        
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void showNoticeDialog() {        
        DialogFragment dialog = new DialogAlex();
        dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog,int id) {        
        //**************HOW obtaining values ​​EditText?*****************************
        Toast.makeText(this,"hola", Toast.LENGTH_LONG).show();      
    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog,int id) {

    }
...

regards

Upvotes: 4

Views: 370

Answers (3)

Manoj J.
Manoj J.

Reputation: 116

Try this code.

public Dialog onCreateDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Get the layout inflater
    LayoutInflater inflater = this.getLayoutInflater();
    Log.i("INFO","Creating Dialog...");
    View view = inflater.inflate(R.layout.goaldialog, null);
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(view)
            // Add action buttons
            .setPositiveButton(R.string.SetAGoalButton, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    EditText et = (EditText)view.findViewById(R.id.GoalChooser);
                    Integer goalNumber = Integer.parseInt(et.getText().toString());
                    globalVariable = goalNumber;
                    Log.i("INFO",Integer.toString(goalNumber));
                    dialog.dismiss();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
    builder.show();
    return builder.create();
}

Upvotes: 0

Blundell
Blundell

Reputation: 76458

You're really close, you have to pass these values through your listener.

Change your interface to this:

public interface NoticeDialogListener {
    public void onDialogPositiveClick(String input1, String input2);
    public void onDialogNegativeClick();
}

Then change onCreateDialog to take advantage of this:

     final View view = inflater.inflate(R.layout.dialog_signin, null);
     builder.setView(view) //This layout contains only two EditText, ET1 and ET2.
                .setPositiveButton("adicionar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        EditText et1 = (EditText) view.findViewById(R.id.et1);
                        EditText et2 = (EditText) view.findViewById(R.id.et2);
                        String input1 = et1.getText().toString();
                        String input2 = et2.getText().toString();
                        mListener.onDialogPositiveClick(input1, input2);
                    }
                })
                .setNegativeButton("cancelar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {        
                        mListener.onDialogNegativeClick();
                    }
                });
        return builder.create();

Then your interface implementation becomes:

@Override
public void onDialogPositiveClick(String input1, String input2) {        
    Toast.makeText(this,"hola " + input1 + " " + input2, Toast.LENGTH_LONG).show();      
}

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157437

you could add it to your interface's methods. E.g.

public interface NoticeDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog, String value, int id);
    public void onDialogNegativeClick(DialogFragment dialog,int id);
}

then

onCreateDialog

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {     
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View view = inflater.inflate(R.layout.dialog_signin, null);
    builder.setView(view)  .setPositiveButton("adicionar", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                   EditText edittext = (EditText) view.findViewById(R.id.editText)
                    mListener.onDialogPositiveClick(DialogAlex.this, edittext.getText().toString(), id);
                }
            })

Upvotes: 1

Related Questions