简体   繁体   中英

Getting value from EditText within an AlertDialog Builder

Right, I know there's alot of similar questions. I've researched on stackoverflow as well as on the internet about this but still stumped.

This code is in a fragment.

...
private Context context = getActivity();

public void Dialog(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    View mView = inflater.inflate(R.layout.dialog, null);
    alertDialogBuilder.setView(mView);

    EditText a = (EditText) mView.findViewById(R.id.a);
    EditText b = (EditText) mView.findViewById(R.id.b);
    EditText c = (EditText) mView.findViewById(R.id.c);

    //a.setText("abc");
    //b.setText("xyz");
    //c.setText("123");

    strA = a.getText().toString();
    strB = b.getText().toString();
    final String strC = c.getText().toString();
}

This should be a typical approach to getting the view of the inflated layout and using it to access the elements inside the view, but it's not working no matter what I tried, I just could not get strA, strB and strC values using getText().toString().

    //a.setText("abc");
    //b.setText("xyz");
    //c.setText("123");

But, if I uncomment the above 3 lines, the values get sent across. And I can receive them inside strA, strB, strC. Why is this so? I don't get it at all.

Any help greatly appreciated, thank you!

Looks to me like you're checking the values of the EditTexts as soon as they're created. Which of course the value is going to be null every time since your user hasn't had time to type anything. I think you need a SubmitButton or something similar that checks the EditTexts in its onClickListener. Though I'll admit I've never inflated a view into an alert dialog.

The layout is not yet initiated when you are trying to set the values, you need to call AlertDialog alertDialog = alertDialogBuilder.create(); and then alertDialog.show(); or just alertDialogBuilder.show() before you can edit the textView content.

You are trying to get values at the time you initialize the dialog. you need to get values after an action, like a button click

alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
     strA = a.getText().toString();
     strB = b.getText().toString();
  }
});

Have a look at this display textview in alert dialog

Try like this. MyDialogue is your Activity name.

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MyDialogue.this);

                // Get the layout inflater
                LayoutInflater inflater = MyDialogue.this.getLayoutInflater();

                // Inflate and set the layout for the dialog
                // Pass null as the parent view because its going in the dialog layout
                View mView = inflater.inflate(R.layout.dialog, null);
                alertDialogBuilder.setView(mView);

                final EditText a = (EditText) mView.findViewById(R.id.a);
                final EditText b = (EditText) mView.findViewById(R.id.b);
                final EditText c = (EditText) mView.findViewById(R.id.c);

                a.setText("abc");
                b.setText("xyz");
                c.setText("123");

                alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                            final String strA = a.getText().toString();
                            final String strB = b.getText().toString();
                            final String strC = c.getText().toString();
                            Log.e("tag", strA + "  " + strB + "  " + strC );
                      }
                    });
                alertDialogBuilder.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM