Java Android AlertDialog SetText
嗨,我有一个alertdialog,当你点击listview中的一个项目时,它会被创建,我试图获取文件名、描述、作者等。。从我的活动中,并在我的alertdialog中显示它,但是.setText将不起作用,请有人帮助。谢谢,这是我的代码:这根本不是在Android中正确使用对话框的方式。您需要在onCreateDialog的覆盖中定义对话框,如文档中所述:Java Android AlertDialog SetText,java,android,android-alertdialog,Java,Android,Android Alertdialog,嗨,我有一个alertdialog,当你点击listview中的一个项目时,它会被创建,我试图获取文件名、描述、作者等。。从我的活动中,并在我的alertdialog中显示它,但是.setText将不起作用,请有人帮助。谢谢,这是我的代码:这根本不是在Android中正确使用对话框的方式。您需要在onCreateDialog的覆盖中定义对话框,如文档中所述: http://developer.android.com/guide/topics/ui/dialogs.html 按照本指南,您应该能
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\u BLOCK\u SIZE)来显示对话框。还要注意,对话框只创建一次,并反复显示。要强制重建对话框,请在调用showDialog(int)之前调用removeDialog(int)。重写onPrepareDialog()是最好的方法,但是使用removeDialog工作起来更容易