我想显示一个 AlertDialog,其中一个选项可能会根据每个请求而更改。因此,例如,有一次我想显示“添加到联系人”选项,而另一次应该显示“从联系人中删除”选项。

我的代码第一次确实可以工作,但是Android似乎缓存了AlertDialog,这样下次就不会执行onCreateDialog。因此该选项不再改变。我可以阻止这种缓存,还是有另一种方法来更改选项?

我正在使用 SDK 1.5,但使用的是 1.1。

@Override
protected Dialog onCreateDialog(final int id) {
    ...
    String add_remove_contact = res.getString(R.string.profile_add_to_contacts);
    if (user.getContacts().contains(profileID)) {
        add_remove_contact = res.getString(R.string.profile_remove_from_contacts);
        // TODO: this string is not changed when contact status changes 
    }
    final CharSequence[] items = {res.getString(R.string.view_profile),
                                  res.getString(R.string.profile_send_message),
                                  add_remove_contact};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    ...
    return builder.create();
}
有帮助吗?

解决方案

您还可以使用 removeDialog(int)的活性的功能。当被驳回的对话,活动基本上存储对话框(由于性能原因我会想象)的状态。调用的 removeDialog(INT)在对话框上迫使活动卸载该对话框的所有引用并从屏幕驳回,如果真实被示出它。

建立对话框结果 活动#removeDialog(INT)

其他提示

看一看示onPrepareDialog 方法将对话之前被调用。有可以根据请求类型改变所需的值。

<强>使用日期选择器实施例

@Override
protected Dialog onCreateDialog(final int id) {
  switch (id) {
  case DIALOG_DATE_ID:
    final Calendar c = Calendar.getInstance();
    return new DatePickerDialog(this, this, c.get(Calendar.YEAR),
                                c.get(Calendar.MONTH), 
                                c.get(Calendar.DAY_OF_MONTH));
  default:
    return super.onCreateDialog(id);
  }
}

@Override
protected void onPrepareDialog(final int id, final Dialog dialog) {
  switch (id) {
  case DIALOG_DATE_ID:
    //update to current time
    final Calendar c = Calendar.getInstance();
    ((DatePickerDialog) dialog).updateDate(c.get(Calendar.YEAR), 
                                           c.get(Calendar.MONTH), 
                                           c.get(Calendar.DAY_OF_MONTH));
    break;
  }
}

这是这个问题的一个DUP: 安卓:无法更改文本显示在AlertDialog

您也可以这样来做: http://andmobidev.blogspot.com/2010/ 03 /修改警报的对话框一览items.html

似乎确实,虽然减慢长按菜单的显示...

我想我有用于上述不一致的行为的修正。最初创建的对话框(如果它仍然是一个AlertDialog.Builder),您必须将信息设置为初始状态(NOT NULL)或onPrepareDialog将不会与预期值覆盖它。因此,当你创建的对话框中,做这样的事情总有消息中的非空值。我与此挣扎天,发现该溶液由事故:

AlertDialog.Builder resultAlert = new AlertDialog.Builder(context);

if ( message == null ) {
    resultAlert.setMessage("");
} else {
    resultAlert.setMessage(message);
}

我了解使用活动管理对话框的性能原因,但建议除简单情况外不要使用它们。其原因有:

  1. Bundle 参数仅在 API Level 8 中添加,因此不能用于向后兼容。这实际上意味着“onPrepareDialog”需要依赖非局部变量来实现状态差异;

  2. 实践表明,响应“onPrepareDialog”主体中所做的任何对话框更改时,行为会很差且不一致。

如果根据需要对对话框进行子类化和创建,则不会出现这些困难。如有必要,可以调用“setOwnerActivity”。

当你有一个自定义对话框,您可以通过使用dialog.getWindow()。findViewById(...)更改自定义项目

这个例子显示保存的最后文本,并再次显示它您显示对话框的下一次。

// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.customized);

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialogInterface) {
       EditText dialogText = (EditText)dialog.getWindow().findViewById(R.id.customText);
       savedText = dialogText.getText();
    }
});

dialog.show();
EditText dialogText = (EditText)dialog.getWindow().findViewById(R.id.customText);
dialogText.setText(savedText);

定制对话框的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<Button
    android:id="@+id/buttonOK"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="OK"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="16dp" />

<EditText
    android:id="@+id/customText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="19dp"
    android:hint="Message"
    android:ems="10"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

和我有一个想法,但不是那么好*的当用户不使用对话相当频繁这是用来的*解决办法:!首先,你要声明一个变量(整型),并默认值0.such为private int i=0; 并使用活性ShowDialog的方法之前,增加int变量i和张贴i的值作为参数作为ShowDialog方法。 的代码可能会喜欢这个

private int i=0;

//before you show the dialog
this.i++;
this.showDialog(this.i);

准确。对于AlertDialog,已创建W / Builder.create()onPrepareDialog()是没用的。 Builder是单向的,一旦被创建对话框,您不能更新。我的意思是不能松散的,我相信你会得到一个处理的观点和手动做这一切,但击败首先使用构建器的点。

我发现的唯一的解决办法是手动创建/显示/关闭该对话框,而不是使用onCreateDialog()showDialog(),等我打过电话removeDialog(),但似乎并没有工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top