How to retrieve value from EditText in AlertDialog?
When selected, a context menu option brings up an AlertDialog. I want the user to enter text into an EditText in the AlertDialog, and when the user presses PositiveButton, the value of EditText is able to be "returned" to the main method. Here is the relevant code from my class:
public class PassPlay extends ListActivity {
public static final int PENALTY_ID = Menu.FIRST+1;
public static final int FUMBLE_ID = Menu.FIRST+2;
public static final int ADDLYDS_ID = Menu.FIRST+3;
public static final int SAFETY_ID = Menu.FIRST+4;
EditText ydsFromAlertDialog;
String penYdsStr;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.passplay);
ydsFromAlertDialog=(EditText)findViewById(R.id.passYdsLabel);
registerForContextMenu(getListView());
}
public boolean onCreateOptionsMenu(Menu menu) {
populateMenu(menu);
return(super.onCreateOptionsMenu(menu));
}
public boolean onOptionsItemSelected(MenuItem item) {
return(applyMenuChoice(item) || super.onOptionsItemSelected(item));
}
public boolean onContextItemSelected(MenuItem item) {
return(applyMenuChoice(item) || super.onContextItemSelected(item));
}
private void populateMenu(Menu menu) {
menu.add(Menu.NONE, PENALTY_ID, Menu.NONE, "Penalty");
menu.add(Menu.NONE, FUMBLE_ID, Menu.NONE, "Fumble");
menu.add(Menu.NONE, ADDLYDS_ID, Menu.NONE, "Additional Yards");
menu.add(Menu.NONE, SAFETY_ID, Menu.NONE, "Safety");
}
private boolean applyMenuChoice(MenuItem item) {
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView;
switch (item.getItemId()) {
case PENALTY_ID:
textEntryView = factory.inflate(R.layout.textdialog, null);
new AlertDialog.Builder(this)
.setView(textEntryView)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.timeout)
.setPositiveButton(R.string.offense, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Actions for offensive timeout
EditText penaltyYds=(EditText)findViewById(R.id.ydsAssessedLabel);
penYdsStr = penaltyYds.getText().toString();
ydsFromAlertDialog.setText(penYdsStr);
}
})
.setNeutralButton(R.string.defense, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Actions for defensive timeout
}
})
.setNegativeButton(R.string.cancel, null)
.show();
return true;
case FUMBLE_ID:
//Fumble window
return true;
case ADDLYDS_ID:
//Additional Yards window
return true;
case SAFETY_ID:
//Safety window
return true;
}
return(false);
}
}
The main XML layout (passplay.xml) has your normal TextViews, EditTexts, CheckBoxes, etc. I want to set one of those EditTexts (ydsFromAlertDialog) to be assigned the value entered in the AlertDialog (EditText penaltyYds). The AlertDialog's XML layout (textdialog.xml) is very simple with one TextView and one EditText.
When I run the program, the following line errors out with "The application has stopped unexpectedly."
penYdsStr = penaltyYds.getText().toString();
So in summary, I want to press the menu option "Penalty", have an AlertDialog with an EditText where I enter a number, and when I press PositiveButton, the EditText ydsFromAlertDialog's value is changed to what was entered in the Dialog.
In reality, I have a database table with 5 columns, 4 of which will be populated by normal fields, but the 开发者_Go百科5th will be populated by the value entered in the Dialog. I figured if I can "return" it to "be with" the rest of the values, I'll be able to save it to same table record as the others, too.
Let me know if you need any more information. Thank you!
You have to get the ydsAssessedLabel from the view you inflated
EditText penaltyYds=(EditText)textEntryView.findViewById(R.id.ydsAssessedLabel);
精彩评论