具有自定义视图的AlertDialog.Builder:如何初始化视图? [英] AlertDialog.Builder with a custom view: how to initialize the view?

查看:115
本文介绍了具有自定义视图的AlertDialog.Builder:如何初始化视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AlertDialog来编辑列表项.我可以使用Builder.setView传递我的编辑器布局,并且可以从肯定按钮回调中获取结果,但是我没有看到初始化视图的方法.例如有一个EditText应该显示一个特定的字符串,依此类推.可以吗?

I'm working on an AlertDialog for editing a list item. I can pass my editor layout with Builder.setView, and I can get the result from the positive button callback, but I don't see a way to initialize the view. E. g. there's an EditText that should be displaying a specific string, and so on. Is it doable?

我意识到我可以继承Dialog的子类,但是看来我需要更多的实用程序代码才能实现相同的结果.似乎不太干净.

I realize I could subclass Dialog, but then it seems I'd need more utility code to achieve the same result. Seems less clean.

推荐答案

简单来说,通过预先扩大视图的引用来保留视图的引用:

Simply, retain a reference of the view by inflating it beforehand:

LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);

然后,获取对您的EditText的引用为:

Then, get a reference to your EditText as:

EditText editText = (EditText) dialogView.findViewById(R.id.label_field);

这篇关于具有自定义视图的AlertDialog.Builder:如何初始化视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆