在下面的代码中,我尝试使用AlertDialog获取文件夹名称的String输入,但是当对话框出现时,Activity不会等待对话框退出并且folderName的值为null。我的问题是如何让它等待输入?
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case 5:
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.prompts,null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);
alertDialogBuilder.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
MainActivity.this.folderName = userInput.getText().toString();
}
}).setNegativeButton("Cancel",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
try
{
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
Log.d("create folder","folder name : "+folderName);
$mkdir(curDir,folderName);
}
catch(Exception e)
{
this.displayToast("Exception : "+e);
}
return true;
}
}
答案 0 :(得分:1)
移动此代码:
Log.d("create folder","folder name : "+folderName);
$mkdir(curDir,folderName);
到
public void onClick(DialogInterface dialog, int id)
{
// Put code here
}
现在您的代码如下:
alertDialogBuilder.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
MainActivity.this.folderName = userInput.getText().toString();
$mkdir(curDir,folderName);
}
})