重置 Android 对话框
我发现了很多关于如何保存对话框状态的主题,例如使用 Bundle 实例。 但是,我似乎找不到如何“正确”重置对话框。
考虑这个示例自定义对话框(XML 布局带有 ID 为“input_text”的 EditText):
public class CustomDialog extends Dialog {
public CustomDialog (Context context) { super (context); }
protected onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.input_query);
EditText txt = (EditText) findViewById(R.id.input_text);
// Consider that I'm declaring here the use of listeners
// in order to retrieve input text.
}
}
当在主 Activity 中单击按钮时,我会弹出此对话框。第二次发生此操作时,我的 EditText 包含前一个实例的输入。
现在,我知道我可以重置此 EditText 的内容,但我想知道是否有一个通用的、已经存在的方法可以在对话框范围内执行此操作。例如,如果我有一个更复杂的对话框,我希望每次实例化该对象时它都有“默认”值。我认为删除对 super.onCreate(savedInstanceState); 的调用可以解决问题,但事实并非如此。
抱歉,如果这是一个愚蠢的问题(或者如果已经解决了......我没有找到它)...任何帮助表示赞赏。多谢!
I found plenty of topics on how to save states of a dialog, for instance using Bundle instances.
However, I can't seem to find how to "properly" reset a dialog.
Consider this sample custom dialog (the XML layout carries an EditText with ID "input_text"):
public class CustomDialog extends Dialog {
public CustomDialog (Context context) { super (context); }
protected onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.input_query);
EditText txt = (EditText) findViewById(R.id.input_text);
// Consider that I'm declaring here the use of listeners
// in order to retrieve input text.
}
}
I'm popping up this dialog when a button is clicked in the main Activity. The second time this action occurs, my EditText contains the input from the previous instance.
Now, I know I can reset the content of this EditText, but I'm wondering if there is a common, already existant method to do this, dialog-wide. For instance if I have a more complex dialog, I want that it has "default" values each time this object is instantiated. I thought removing the call tosuper.onCreate(savedInstanceState);
could do the trick, but it does not.
Sorry if this is a silly question (or if already addressed... I did not find it)... Any help is appreciated. Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常您应该重写 Activity 的
onCreateDialog
和onPrepareDialog
方法。第一次创建对话框时,会调用
onCreateDialog
,因此您应该在此方法中创建对话框。要初始化/更新对话框的内容,请覆盖onPrepareDialog
,它始终在显示对话框之前调用。Normally you should override
onCreateDialog
andonPrepareDialog
methods of the activity.For the first time creation of dialogs,
onCreateDialog
is called, so you should create your dialog in this method. To initialize/update the contents of dialogs overrideonPrepareDialog
, it is always called before showing a dialog.尝试使用与 showDialog() 相反的方法 - removeDialog() 就是该方法。我遇到了与你类似的问题。我显示的每个对话框都包含来自第一个实例的数据。当我开始使用removeDialog()时,内容正在正确更新。
Try using method opposite to showDialog() - removeDialog() is that method. I was having problem similar to yours. Every dialog i was showing was having data from the first instance. When I started to use removeDialog() the content was being updated properly.