【问题标题】:onCreateDialog and dynamic dialogs (Android)onCreateDialog 和动态对话框 (Android)
【发布时间】:2011-10-18 11:09:00
【问题描述】:

我有一个小问题。在我的程序中我定义了

protected Dialog onCreateDialog(int id) {
    if (id == CONTEXT_MENU_ID) {
        return createMyDialog();
    }
    return super.onCreateDialog(id);
}

然后显示对话框调用

showDialog(CONTEXT_MENU_ID)

我的问题是有时我想在执行之间动态更改对话框的文本。但是使用这种方法,对话框永远不会重新创建。如何在显示对话框之前调用 createMyDialog()?

谢谢

【问题讨论】:

  • 动态?严重地? ;)

标签: java android dialog


【解决方案1】:

如果您想更改对话框设置(文本等),您需要在onPrepareDialogMethod 中进行,每次调用showDialog 方法时都会调用它

【讨论】:

    【解决方案2】:

    这可能值得一试。我还没有测试出来。如果将 textview 设置为对话框的内容,则可以为其设置 id。

    TextView text = new TextView(this);
    ViewGroup.LayoutParams vp = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    text.setLayoutParams(vp);
    text.setText("HI");
    text.setId(1005);    
    dialog.setContentView(text);
    

    所以下次当你尝试更新 textview 时,你也许可以使用 id 来访问它。

    ((TextView)dialog.getWindow().getDecorView().findViewById(1005))
            .setText("New Text");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多