gpt4 book ai didi

java - Android AlertDialog 设置文本

转载 作者:太空狗 更新时间:2023-10-29 14:31:52 25 4
gpt4 key购买 nike

您好,当您单击 ListView 中的项目时,我会创建一个警报对话框,我试图从我的 Activity 中获取文件名、描述、作者等,并将其显示在我的警报对话框中,但是 .setText 将不工作,有人可以帮忙。谢谢,这是我的代码:http://pastebin.com/FzWSPp5e

最佳答案

这根本不是您在 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 更容易。

关于java - Android AlertDialog 设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5825711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com