选择后,上下文菜单选项会显示AlertDialog。我希望用户在AlertDialog中的EditText中输入文本,当用户按下PositiveButton时,EditText的值可以“返回”到main方法。这是我班上的相关代码:
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);
}
}
主XML布局(passplay.xml)有你的普通TextViews,EditTexts,CheckBoxes等。我想设置其中一个EditTexts(ydsFromAlertDialog)来赋予在AlertDialog(EditText penaltyYds)中输入的值。 AlertDialog的XML布局(textdialog.xml)非常简单,只有一个TextView和一个EditText。
当我运行程序时,以下行错误显示“应用程序意外停止。”
penYdsStr = penaltyYds.getText().toString();
总而言之,我想按菜单选项“Penalty”,使用带有EditText的AlertDialog输入一个数字,当我按下PositiveButton时,EditText ydsFromAlertDialog的值将更改为在Dialog中输入的值。
实际上,我有一个包含5列的数据库表,其中4列将由普通字段填充,但第5列将由对话框中输入的值填充。我想如果我可以“返回”它“与其余的值一起”,我将能够将它保存到与其他值相同的表记录中。
如果您需要更多信息,请与我们联系。谢谢!
答案 0 :(得分:4)
您必须从充气的视图中获取ydsAssessedLabel
EditText penaltyYds=(EditText)textEntryView.findViewById(R.id.ydsAssessedLabel);