Return data from dialog
I am using a dialog as a form for the user to input data. When they click the "OK" button, the dialog is closed, and I need to use the data that was entered. How can I reference this data in the activity once the dialo开发者_StackOverflowg has closed?
Get it when the user presses the "OK" button
final EditText input = new EditText(this); // This could also come from an xml resource, in which case you would use findViewById() to access the input
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
mItem.setValue(value); // mItem is a member variable in your Activity
dialog.dismiss();
}
});
Assign an OnDismissListener to the dialog and pass the data to the activity there.
Alternatively, you can create a dialog activity and return the data as the activity result. See the following link for more info about starting activities and gettings results:
http://developer.android.com/reference/android/app/Activity.html#StartingActivities
精彩评论