您好我有一个alertdialog,当您点击列表视图中的项目时创建,我试图从我的活动中获取文件,描述,作者等的名称..并在我的alertdialog中显示它但.setText将没有工作,有人可以帮忙。谢谢,这是我的代码:http://pastebin.com/FzWSPp5e
答案 0 :(得分:1)
这完全不是你如何正确使用Android中的对话框。您需要在onCreateDialog的覆盖中定义对话框,如文档中所述:
http://developer.android.com/guide/topics/ui/dialogs.html
按照本指南,您应该能够解决问题。这是我刚从一个随机应用中复制和粘贴的一个例子:
@Override
protected Dialog onCreateDialog(int id, Bundle b) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder builder = null;
switch(id) {
case DIALOG_BLOCK_SIZE:
{
Dialog dialog = new Dialog(this);
final View dialogLayout = inflater.inflate(R.layout.dialog_block_size, null);
builder = new AlertDialog.Builder(this);
builder.setView(dialogLayout);
builder.setTitle("Set Block Size");
final EditText blockIn = (EditText)dialogLayout.findViewById(R.id.block_size_in);
blockIn.setText(new Integer(pref.getInt("block_size", 6)).toString());
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SharedPreferences.Editor editor = pref.edit();
editor.putInt("block_size", new Integer(blockIn.getText().toString()));
editor.commit();
////////TODO///////////
//notify MinutemaidService that we have changed the block_size
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog = builder.create();
return dialog;
}
default:
{
return null;
}
}
return dialog;
}
使用上面的代码,您可以调用showDialog(DIALOG_BLOCK_SIZE)来显示对话框。还要注意,对话框只创建一次并反复显示。要强制重建对话框,请在调用showDialog(int)之前调用removeDialog(int)。重写onPrepareDialog()是最好的方法,但使用removeDialog可以工作,而且更容易。