/ *我有一些奇怪的问题,在OnMenuItemClickListener中,我正在调用我做的警告对话框,但似乎,当我调用警告对话框时,它不会在正确的时刻显示它,只有在onMenuItemClick完成之后。我在做什么? * /
class MyListMenuListener implements OnMenuItemClickListener
{
private String TAG;
@Override
public boolean onMenuItemClick(MenuItem item)
{
if (item.getItemId() == saveRoute.getItemId())
{
alertDialogSaveFile();
//nameInput = "testone.txt";
//some operations
// ...
// return true;
}
// ...
/*the wierd thing is that the alert dialog doesnt show up on the same moment i call it..
only after the onMenuItemClick operation ends (with return)
and this is how my alertdialog looks like:*/
private void alertDialogSaveFile()
{
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Save your current map");
alert.setMessage("Please insert name for this route");
final EditText saveNameInput = new EditText(TwittListActivity.this);
alert.setView(saveNameInput);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
nameInput = saveNameInput.getText().toString();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
}
});
AlertDialog ad = alert.create();
ad.show();
}
//Thanks!
//ray.
答案 0 :(得分:3)
Android中的对话框不是同步的,因此在创建/显示对话框之前,UI线程很可能正在完成onMenuItemClick()
(对话框由封闭的Activity管理)。
编辑:我误解了你的部分问题。保持对alertDialogSaveFile()
的调用,并将实际保存文件的代码放入onClick()
处理程序。由于Android中的对话框不是同步的,因此您需要在对话框回调本身中执行保存操作。无法显示对话框,等待用户响应,然后从对话框中获取结果。